North Idaho Linux Users Group




Writing a Shell Script

1. Tools to use

To write a shell script, use a standard text editor. That can be a command line editor, such as vi, emacs, pico, ted or joe. It can also be a GUI editor, such as Gedit, Kate, Kwrite, Kedit or Jedit.

Shell Scripts always start with a line telling the shell what program to use to execute the script. The line always starts with a shebang and the path to the program that will execute the script:


The first line would invoke the Bourne Again Shell. The second line would invoke the generic Bourne Shell.

2. Executing a shell script

Once a shell script file is written and saved, you must tell the the shell that it is actually executable, not just another text file. You do this by changing the file permissions:

chmod +x
chmod 755

Both of the above commands set the file to be executable. If you have saved the file someplace where root access is required to change permissions, use

sudo chmod +x
sudo chmod 755

To execute the script, you must tell the shell to run it. # script is in the shell PATH
/tmp/ # script is not anywhere known
./ # script is in the current directory

If the file has been saved somewhere on the path (echo $PATH to see where the shell looks), then just typing the name of the shell script will execute it. If the file has been saved somewhere else, type the full path to the file. If the file is in the current directory, type a './' before the name.

3. Writing the script

A shell script just contains a list of commands you could just as well type directly on the command line. The commands can be external programs or internal shell commands. If the list is long or the commands are long, it is easier to put them in a script and execute the script with a single command. Here is a simple '' script:

1: #!/bin/bash
2: #
3: #
4: #
5: clear
6: echo "Hello, $USER"
7: echo "Today is"
8: date
9: echo "This is an $HOSTTYPE machine"
10: echo "Number of users logged in:"
11: who | wc -l
12: echo "This month's calendar:"
13: cal
14: exit 0

Line 1 tells the command line shell to use bash to execute this script.

Since anything after a single # is considered a comment and is ignored, lines 2 through 4 are ignored.

Line 5 is a Linux command that is used to clear the terminal display.

All shells have certain 'built-in' commands. The echo command is one of those 'built-ins'. It prints the line to the display. Line 6 uses the echo command to display the line. Note the $USER on this line. All shells also have items called 'environment variables'. The USER variable is one that is always present. It contains the name of the person who is logged in. The $ is used to tell the shell that this is an environment variable and that we want to use its contents. If you want to see all the environment variables, just type 'set' (the 'set' command is used to display, set and clear the variables.)

Like line 6, line 7 echoes its contents.

Line 8 executes the Linux 'date' command to display the current date and time.

Lines 9 and 10 echo their contents.

Line 11 demonstrates how to pipe the output of one Linux command into the input of another Linux command. The 'who' command prints a line for each use logged in to the machine. Those lines are piped into the 'wc -l', where the wordcount program counts the number of lines (-l) and displays the result.

Line 12 echoes its contents.

Line 13 is a Linux command that displays a calendar. With no arguments, it will display a calendar for the current month.

Line 14 is optional, but a good idea to include. All shell scripts return a value to the shell they are running under. If that return value is zero, then everything has gone all right in the script. If it is any other value, it's considered an error. If you don't include the 'exit', the script will return the results of the last command executed (the 'cal' command in our example).

4. Shell Variables

When processing data in a script, it is often necessary to refer back to values created earlier in the script, or to values created by Linux itself. These values are stored in places called shell variables. Shell variables are untyped: they can be treated as either alphanumeric strings of characters or as numbers, depending on whether they actually ARE numbers.

Each variable has a label. That variable is invoked by the shell script by using the label with a dollar sign ($) pre-pended to the label. For instance, the PATH variable is used as $PATH. If you neglected to put that dollar sign in front, the shell script would assume you wanted to use the word PATH. Important note: shell variables are case-sensitive. PATH, Path and path are all different variables.

There are a lot of built-in shell variables other than the ones we have already mentioned. Here are a few more that may be of interest.

5. Built-in Commands

While the command shell can execute any program you tell it (and it can find), its real power comes from using commands that are built into the shell program. These internal commands allow you to do things like changing environment variables, sending information to the display, make other changes to the shell environment and, most importantly, control the flow of your shell program.

Some of the environment-type commands that are built into the shell are

The program flow commands are

Here is a simple script

1: #!/bin/bash
2: #
3: #
4: #
5: echo "What is your name?"
6: read yourname
7: if [ $yourname == "mike" ]; then
8: echo "Hey, $yourname, I know you!"
9: else
10: echo "Hello, $yourname, I'm happy to meet you."
11: fi
12: exit 0

We have already talked about lines 1 through 4 in our first example.

Line 5 is the prompt for user input.

Line 6 reads a line from the keyboard and puts it in the shell variable 'yourname'.

Line 7 tests the contents of yourname against the string 'mike'. if they are equal, line 8 is executed, then line 11. If the test is false, line 10 is executed, then line 11.

Line 12 is our normal exit from the program.

6. Towers of Hanoi Example (

The Towers of Hanoi program demonstrates the use of the case statement, subroutines and program recursion.

#! /bin/bash
# The Towers Of Hanoi
# Bash script
# Copyright (C) 2000 Amit Singh. All Rights Reserved.
# Modified by M. Burton

# Last tested under bash version 2.05b.0(13)-release

dohanoi() {
case $1 in
dohanoi "$(($1-1))" $2 $4 $3
let "STEP=STEP + 1"
echo ${STEP}: move $2 "-->" $3
dohanoi "$(($1-1))" $4 $3 $2

case $# in
case $(($1>0)) in

dohanoi $1 1 3 2
exit 0;
echo "$0: illegal value for number of disks";
exit 2;
echo "usage: $0 N"

exit 1;