BASH Shell Script Examples

BASH_SHELL contains some examples of useful or interesting BASH Shell scripts.

"BASH" is an acronym for "Born-Again SHell" or "Bourne Again SHell", because it is a shell language "born again" from the older Bourne shell. People who worry about that sort of thing will tell you that the phrase "BASH Shell" is incorrect, since the "SH" already stands for shell. They will then get money from the ATM machine.


A BASH Shell script is usually stored in a file whose extension is .sh.

The first line of a BASH Shell script should usually be

which guarantees, no matter what shell you are running, that the commands in the script will be processed by the BASH Shell.

A BASH Shell script can be executed by giving it as input to the BASH Shell interpreter:


If the shell script has been made executable:

chmod +x
then it can be run directly, as though it were a command:
or, if your current directory is not in your path, perhaps

Related Data and Programs:

C_SHELL, examples which illustrate the use of C shell scripts.

SGE, examples which illustrate the use of the Sun Grid Engine (SGE), a job queueing system whose input is a modified form of shell scripts in the BASH shell, C shell, and so on.


  1. Cameron Newham, Bill Rosenblatt,
    Learning the BASH Shell,
    Third Edition,
    O'Reilly, 2005,
    ISBN: 978-0-596-00965-6,
    LC: QA76.O63.N458.

Examples and Tests:

EPS_TO_PDF automates the task of converting EPS files to PDF format with the convert() command. Ordinarily, if you had 50 EPS files in a directory, you would have to issue 50 convert commands, of the form

        convert file.eps file.pdf
This shell script automates this task, and illustrates how the BASH "for" statement can range over every file that matches an "ls" command; how the "first name" of a file can be extracted, how two strings can be concatenated, and how an integer variable can be used to count the number of files successfully converted.

COLLATZ_COUNT uses a Perl script to count the number of terms in the Collatz sequence for a given value of N. Since several values of N are of interest, we write a BASH script which uses a DO loop to execute the script with each value.

FOR_STRING shows how a FOR loop can be used to process a list of string items. In this case, we simply want to say happy birthday to John and Bob and Mary, but in a real application, we might want to carry out a sequence of operations to /usr/work/$USER and to /usr/home/$USER and to /usr/temp/$USER, for instance.

HELLO_WORLD uses the echo command to say "Hello, world!".

You can go up one level to the EXAMPLES page.

Last revised on 09 April 2018.