Some BASH Basics

Before we get ahead of ourselves, it would probably be a good idea to go over some BASH basics.

Let's start at the beginning, OK? The Bourne Shell was born at AT&T's Bell Laboratories a while back. It was birthed by Steve Bourne, hence the name. The Bourne Again Shell (bash) is a direct descendent of the Bourne Shell (sh). It's very similar to the critter that Mr. Bourne brought to life back at Bell Labs.

There are different types of shells for different purposes. You can have a login shell, which is interactive; a non-login shell, which is also interactive; and a non-interactive shell, like the one used to execute a shell script.

The characteristics of these shells are usually determined by a file in the user's /home directory called .bashrc. It's sort of like a configuration file in that items place within it will be faithfully followed by the shell when it is initialized. We've seen this already when we were pimping our BASH prompt in a previous article here. I'm over-simplifying, of course. There are other files, be they root or user oriented, that also affect BASH's behavior. However, we don't need to go into that at the moment. For now we just want to get a bit more familiar with BASH.

Symbols are an important part of BASH scripting. Some commonly used ones are (, ), [, ], and $. You can see them in action in this snippet of a script:


initialize_Suits ()
Suits[0]=C #Clubs
Suits[1]=D #Diamonds
Suits[2]=H #Hearts
Suits[3]=S #Spades

initialize_Cards ()
Cards=(2 3 4 5 6 7 8 9 10 J Q K A)
# Alternate method of initializing an array.

Standard Input, Standard Output, Standard Error... You may run across these terms while experimenting with and learning BASH. The first is usually provided by your keyboard; typing, in other words. The second is just the printed reply that BASH gives you in the command line. The third is the standard error notice that BASH will give you when it can't find something or follow a command you've entered. Here's an example of an error:

$ cat jimy_grades.txt

cat: jimy_grades.txt: No such file or directory

You list the contents of your working directory and find that you mis-spelled that file. It's actually jimmy_grades.txt. This is why the cat command could not find it and BASH provided that standard error output for you. You can also redirect inputs and outputs in BASH using the | and < > symbols. We'll see redirection in action a bit more later on when we write a few simple scripts to do stuff.

What is a shell script? Well, it's a file that tells the shell (BASH, in our case) to do something. How does it do this? We “code” or write step-by-step commands and subcommands within the script, using variables and flow control, to make the shell follow these logical steps to achieve our goal.

You can write scripts to do just about anything on your systems. For example, you can write scripts that automate backup or updating chores, etc. You can write scripts to randomly change your desktop wallpaper. You can write scripts that do mulitple chores for you; some become quite multitasking in nature, with more than just a single result. Scripts can be very useful tools.

Writing code, or scripting, is like learning any other human language. It takes time and effort to learn... and lots of practice. You have to practice what you learn or you'll lose it the same way folks lose their second languages if they don't speak or read/write them regularly.

We made a simple script in yesterday's lesson. We showed how Mary was able to write a small script at her workplace that simplified a chore that she had to perform often during the day. We'll move ahead in the coming days to more complicated scripting, but nothing too complicated. The goal here is just to familiarize you with shell scripting, not to make you and expert. Only you can make you an expert.

More soon...


Note: As always, please remember to click on the embedded links within my articles for definitions to unusual words, historical background, or links to supplemental informative sites, etc.

*Note: this article originally published on my Nocturnal Slacker v1.0 at site.




Subscribe to Comments Feed

Upcoming Linux Foundation Courses

  1. LFS220 Linux System Administration
    30 Nov » 03 Dec - Virtual
  2. LFS416 Linux Security
    07 Dec » 10 Dec - Virtual
  3. LFD411 Embedded Linux Development
    07 Dec » 11 Dec - Atlanta

View All Upcoming Courses

Who we are ?

The Linux Foundation is a non-profit consortium dedicated to the growth of Linux.

More About the foundation...

Frequent Questions

Linux Training / Board