Bash Shell ScriptingLast week, we discovered the benefits of shell scripting, and got our first glimpse at an example script.

This week we’re going to dissect the sample script and learn about the various components that make it tick.

To re-iterate, the example script goes something like this:

#!/bin/bash
# My First Bash Shell Script
clear
ls -l
echo "Can you see me?"
echo "Yes? Ok, let's move on."
exit 0

Now, let’s see what all we’re dealing with.

The Shebang

The “shebang” (pronounced sha-bang) is a combination of two characters (a pound sign and an exclamation mark) that are located at the very beginning (first line) of a script.

The shebang is used to indicate to the shell which interpreter should execute the script, so it should be immediately followed (in the case of shell scripts) by /bin/bash to indicate the full path to the interpreter, and identify the file as a shell script.

The full line is:

#!/bin/bash

Other types of scripts can be identified by using the shebang with paths to their relevant interpreters. Examples: #!/usr/bin/perl and #!/usr/bin/python

The term “shebang” comes from a combination of the two character names. The pound sign is affectionately called a “hash”, and the exclamation mark is called a “bang”. When you combine the last part of “hash” with “bang”, you get “shbang”, or “shebang”.

Comments

Comments are blocks of text inside a script that are ignored by the interpreter.

They allow for explanations to be added that break down each part of a script and clarify what each part is doing.

Comments are especially important in long scripts and/or scripts that will be seen by 2nd/3rd/etc. parties beyond their author.

(They’re also very useful to the author themselves, after enough time has passed that we can no longer remember details about a script!)

To make a comment, use a pound sign at the beginning of a line, followed by a space, after which any text will be ignored (by the interpreter) up to the end of the line.

For long comments, break up the text over multiple lines, and remember to begin each line with “# “.

# This is a valid comment.
# This command will not be interpreted: ls -al
# This is a fairly short comment that is just barely long
# enough to get my point across about breaking up long comments.

Although comments and shebangs both begin with a pound sign, they are set apart by the fact that the comment pound sign is followed by a space, and the shebang pound sign os followed by an exclamation mark.

Script Content

The actual content of the script — the part which does things — goes in-between the shebang and the end (or the exit code) of the script.

This part may or may not include comments, as needed, and will be composed of various commands, variables, conditions, etc.

Each new line is treated as a new command.

Exiting a Shell Script

While not strictly required on the average script, proper exit etiquette is a good habit to develop.

Exit codes terminate shell scripts, and indicate at the same time whether or not a script has executed successfully.

An exit code of 0 indicates zero errors, or successful execution. A script can end with exit 0 to indicate successful execution, if you are assured of its success.

An exit code of 1 indicates that there are errors — or at least one error — within the execution of the script. Later on, when you learn about tests (conditions), you will understand better how this can be used and useful.

Several other exit codes exist, but they are rare enough as to be outside the scope of our topic just now.

Summary

Shell scripts are interpreted from top to bottom, left to right, just like you might expect.

The various components and commands that make up a script should be placed in the order that you want them to be carried out.

Each script should begin with the shebang, end with exit, and contain various commands and comments in between.