Intro to Bash

Bash stands for the Bourne Again SHell. The term Bash is yet another term that begs disambiguation, and - when used or heard - the context of the use is key to the specific meaning of the term. Bash can mean, at any point, one or more of the following:

  1. A shell environment.
  2. A type of script or scripting language.
  3. A collection of built-in commands available within the terminal.

The following quote helps clarify what Bash can mean when used.

Bash is the GNU Project's shell. Bash is the Bourne Again SHell. Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use. In addition, most sh scripts can be run by Bash without modification.

--GNU Bash Home

This class will show how to write a Bash script and run it manually as well as automatically on a schedule. We will talk about what the 'SHELL' is, as well as the environment via the command `env`. After an introduction to what Bash is and what scripts are compsed of, the class will focus on several pre-written Example BASH Scripts .

Installing Bash

Bash is the default shell in Red Hat and most derivatives. No additional installation necessary.

Bash Configuration

The Bash utility needs no configuration per se. There are several files that tell Bash what to do under certain circumstances. See the section called "User Configuration Files" for more details.

For our purposes, we'll consider what it takes to create a workable script. Listed below are the crucial elements for an executable script:

  1. A file with the magic 16 bits configured properly.
  2. One or more commands or directives within the file.
  3. The execute bit set on the file.

Figure 6.1. Bash Script Header

Bash Script Header

The program listing below shows a different type of script header. Note that the magic 16 bits are present, as well as the path to the executable that will actually execute the script.

    bob@entanglement ~/bin/
    --> cat ts.sh 
    #!/bin/bash
    ############################################################
    ######################### SECTION #1 #######################
    ###################### header section ######################
    ...

The listing below shows the declaration of variables, which are containers for values that can change as needed.

declare -x  TIMEZONE="0" # timezone var at prompt
declare -x  FORMATTED_DATE="0" # assembled date for input to command
declare -x  TS= # timestamp at the prompt

Please download Example BASH Scripts , and untar them. The following sequence is how we will proceed:

  1. Type the command `cd` to change to your home directory.
  2. Type `ls -al | grep bin`. If you have a bin directory, good.
  3. If you do not have a bin directory, type `mkdir bin`.
  4. Click on the link above and save the scripts to the '~/bin' directory.
  5. Untar the scripts: `tar xvzf scripts.tgz`.
  6. We'll consider the scripts one at a time in class.
  7. Once we've finished looking at the scripts, we'll create our own script.
  8. We'll set our script to run automatically via a cron job with the command `crontab -e`.

Commands to Manipulate Bash

For help with Bash, let's consider the following commands and their output. We'll talk about these at length.

  1. `man bash`
  2. `man -k bash`
  3. `man builtins`

Additional Notes & Considerations for Bash

Bash as a scripting language can be terse to the point of stubborn. Often the difference of the type of quotation mark that is used can mean the difference between the script working properly or not. The following two references provide some of the best, orderly information about Bash and scripting in general.

Reference Material for this Chapter

For this chapter's supporting material, please reference Chapters 2 & 3 in the RHCSA/RHCE Linux Certification Study Guide text book.