Bash Interpreter

Bash Shell Command Interpreter

Bash Command Interpreter


When you type a command at your command prompt you normally execute the command typed by pressing the "Enter" Carriage-Return key. The carriage return is recognised by the shell as an end of line marker. Upon receipt of this marker, the shell will now begin to process the contents of the passed line. Basically, the shell scans the text that has been typed and recreates this string of commands in memory. The shell scans the line one character at a time from left to right. During this scanning process, the shell will process any special characters (variables, wildcards etc..) and recreate and store the command accordingly. The easiest way to illustrate this is to consider the following command:

ls $HOME/scripts/*.txt

The above command is a simple list command, however, the shell has to interpret any special characters that it finds during its scanning process. In this example, the shell will recognise the "$" and the "*" as being a variable and a wildcard special character. The shell will now re-construct the line before it is executed:



ls /home/john/scripts/file1.txt /home/john/scripts/file2.txt /home/john/scripts/file3.txt

The output from the above command will be as follows:


john@john-desktop:~/scripts$ ls $HOME/scripts/*.txt
/home/john/scripts/file1.txt  /home/john/scripts/file3.txt
/home/john/scripts/file2.txt



Filename Wildcard Expansion


As we saw in the previous example a wildcard was used to match any files that had the ending ".txt". Wildcard expansion with filenames can be used to specify a range of characters or exclude a range of characters. Below are list of commonly used wildcard special characters:


Wildcard Description
* Matches zero or more characters
? Matches any single character
[abcd] Match any single character in the list
[a-d] Matches any single character specified within the range
[!abcd] Matches any single character except for characters specified within the range

Examples of Filename Wildcard Expansion


The following files are located within the current directory:



file1.log  file1.txt  file2.log  file2.txt  file3.log  file3.txt

The command "ls *.log" matches all files that end ".log":



john@john-desktop:~/scripts$ ls *.log
file1.log  file2.log  file3.log

The command "ls *.txt" matches all files that end ".txt":



john@john-desktop:~/scripts$ ls *.txt
file1.txt  file2.txt  file3.txt

The command "ls file[12].*" matches any filenames that begin with "file" followed by a "1 or a 2":



john@john-desktop:~/scripts$ ls file[12].*
file1.log  file1.txt  file2.log  file2.txt

The command "ls file[!12].*" matches any filenames that begin with "file" but do not have the character "1 or 2" immediately after:



john@john-desktop:~/scripts$ ls file[!12].*
file3.log  file3.txt

The command "ls ?ile*" matches any file that starts with any single character followed by the characters "ile":



john@john-desktop:~/scripts$ ls ?ile*
file1.log  file1.txt  file2.log  file2.txt  file3.log  file3.txt


Variable Expansion


A variable is simply a place where we can store information in memory. You can create your own variables or extract information from system variables. Variables can be recognised as generally beginning with a "$" sign and containing text that is normally in upper case (Capitals), however, they do not have to be uppercase.

To view the contents of a variable we can use the "echo" command. In the example below I will manually assign my name to the variable $NAME then display its contents:



john@john-desktop:~/scripts$ echo $NAME
John
john@john-desktop:~/scripts$ echo ${NAME}
John

You can also add text around a variable by using Double Quotes. The double quote will allow the shell to expand the variable, however a single quote will not expand the variable:



john@john-desktop:~/scripts$ NAME=John
john@john-desktop:~/scripts$ echo "My name is $NAME"
My name is John
john@john-desktop:~/scripts$ echo 'My name is $NAME'
My name is $NAME

Pre-defined Variables


As we mentioned earlier, there are many system variables that have been pre-defined that contain useful information. To view these variables you can issue the command "env". To view individual variables, we can display their contents using the echo command and by prefixing the variable name with a "$" sign:



john@john-desktop:~/scripts$ echo $HOME
/home/john
john@john-desktop:~/scripts$ echo $PWD
/home/john/scripts
john@john-desktop:~/scripts$ echo "My home area is $HOME. I am currently in the $PWD directory"
My home area is /home/john. I am currently in the /home/john/scripts directory


Command Substitution


When the shell encounters either `cmd` or $(cmd) metacharacter the specified command is executed and the output is placed back on the line. It is often very useful to assign the output of a command directly to a variable:



john@john-desktop:~/scripts$ date
Wed Oct  9 12:21:28 BST 2013

john@john-desktop:~/scripts$ TODAY=`date`
john@john-desktop:~/scripts$ echo $TODAY
Wed Oct 9 12:21:57 BST 2013

john@john-desktop:~/scripts$ TODAY=$(date)
john@john-desktop:~/scripts$ echo $TODAY
Wed Oct 9 12:22:33 BST 2013

In the above example we have used the output from the data command and assigned it to the variable $TODAY. Although there are two methods of assigning the output from a command to a variable, the preferred method is to use the $(cmd) method as opposed to using the back ticks `cmd`. This is due to the fact that the "back ticks" can easily be mistaken for single quotes.


Quotes


As we have seen from some of the examples above, various quotes can have a significant effect on the output that is displayed. Below is a quick summary of various quotes that can be sued and their purpose:


Single Quote ' '


The single quote is used to stop the shell from recognising any special characters. (except for the single quote itself!).



john@john-desktop:~/scripts$ echo 'My Name is $NAME'
My Name is $NAME


Double Quote " "


A double quote is used generally to protect certain special characters with the exception of the back slash \ and the dollar $.



john@john-desktop:~/scripts$ echo "My Name is $NAME"
My Name is John


Back Slash - Escape Character


The "\" character is used to protect the immediate following character. The "\" stops the shell from recognising the next character as special.



john@john-desktop:~/scripts$ echo "\\"
\

In the above example, we used the "\" to protect the following character. Try removing the first "\" . Below is another example indicating how double quotes can be displayed:



john@john-desktop:~/scripts$ echo ""Today is $(date)""
Today is Wed Oct 9 12:52:05 BST 2013
john@john-desktop:~/scripts$ echo "\"Today is $(date)"\"
"Today is Wed Oct  9 12:52:21 BST 2013"