Chapter 7. Bash Scripting

Table of Contents

7.1. The bash environment
7.1.1. Variables
7.1.2. Configuration files
7.2. Scripting Essentials
7.2.1. The script file
7.2.2. Passing variables to the script
7.2.3. Special Variables
7.3. Logical evaluations
7.4. Flow Control and Loops
7.4.1. if then
7.4.2. while loop
7.4.3. until loop
7.4.4. for loop
7.5. Expecting user input
7.5.1. Using case
7.5.2. Using select
7.6. Working with Numbers
7.6.1. Binary operations
7.6.2. Comparing values
7.7. Exercises and Summary
7.7.1. Questions
7.7.2. Summary
7.7.3. Exercises

Prerequisites

Goals

7.1. The bash environment

7.1.1. Variables

When you type a command at the prompt the bash shell will use the PATH variable to find which executable on the system you want to run. You can check the value of PATHpath using the echo command:

$ echo $PATH
/usr/bin:/bin:/usr/sbin:/usr/X11R6/bin:/sbin/

In fact many variables are needed by the shell to accommodate for each user's environment. For example PWD, HOME, TERM and DISPLAY are such variables.

To initialise and declare a variable the syntax is as follows:

VARIABLE=VALUE

Remember not to put any spaces around the = sign. Once a variable is declared and initialised it can be referenced by using the dollar symbol in front as here:

echo $VARIABLE

This declares a local variable (only available for the current process) that can be listed with set. It is possible to get an exported variable (available to all child processes spawned after the variable has been defined) using export. Exported variables are listed with the env command.

When a shell session is started a number of configuration files are read and most of the variables are set.

To free a variable from its current value use unset.

7.1.2. Configuration files

One can distinguish configuration files which are read at login time and configuration files which are read for each new bash session.

7.1.2.1. The profiles

The first file to be read at login is /etc/profile, after that the shell will search for the files ~/.bash_profile, ~/.bash_login and ~/.profile and execute the commands from the first available one. For every new shell (for example if an xterm emulator is started) these profiles are not read again.

The profiles are used to define exported variable (eg PATH) that will be available for every subsequent program.

7.1.2.2. The bashrc files

The runtime control files ~/.bashrc and /etc/bashrc are sourced every time a shell is started.

The runtime control files will store aliases and functions.

7.1.2.3. Reading of Configuration files

Notice that non-interactive shells read neither of these files. Instead a BASH_ENV variable pointing to the file to be sourced is declared in the script.

Function Syntax

function-name ()
{
 command1;
 command2;
}

You can test which files are being read by adding an echo Profile line in /etc/profile, the type:

bash         # no profile is read, nothing is printed
bash -login  # forces bash to read al files, prints Profile

The following commands also control the way bash starts and can be used for testing:

bash -norc
bash -noprofile

Notice that any new bash session will inherit the parent's global variables defined in /etc/profile and ~/.bash_profile.

7.1.2.4. Controlling readline

The GNU library readline is used by program that expect user input. It also offers extensive vi and emacs style editing functionality.

Example: the readline default editor setting for bash is emacs. One can for example use Ctrl+E to go to the end of a line. What happens when we next start, as below, a shell without editing support?

bash --noediting  

The files /etc/inputrc or ~/.inputrc are used to control the readline library. One can for example link a keyboard combination to an action.

Example options for inputrc:

set editing-mode vi    # change the initial editor style 
Control-o: "> output"  # Ctrl+o outputs the string > output
TAB: complete          # command and file auto-completion
set bell-style none    # errors warnings are not audible 

Finally, when a user logs out, the shell will read commands from ~/.bash_logout if it exists. This file usually contains the clear command which clears the screen once the shell exits.