How to Write a Basic Shell Script

A shell is a command line interpretor, which takes commands and executes them. If you find that you execute a sequence of commands frequently in your command line, you can write a shell script to automate this process. A shell script is a text file with a sequence of commands in it that are interpreted/executed by the shell. In this tutorial, we explain how to write a shell script for Bourne shell (or Bash).

Suppose you often run the following sequence of the commands in your shell

$ date
Tue Jun 12 11:50:23 PDT 2007
$ who
cho      tty0         Jun  5 10:52 (:0)
$ ps ax
      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
I    5500    2796    5500       4492    0 1003   Jun  5 /usr/bin/bash
     4804    4068    4804       5496    2 1003 11:50:28 /usr/bin/ps
and you find it tedious to repeatedly type the same sequence of commands. Wouldn't it be wonderful to simply type a single command, say, dwp like
$ dwp
Tue Jun 12 11:50:23 PDT 2007
cho      tty0         Jun  5 10:52 (:0)
      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
I    5500    2796    5500       4492    0 1003   Jun  5 /usr/bin/bash
     4804    4068    4804       5496    2 1003 11:50:28 /usr/bin/ps
and execute all the commands like above?

This is indeed possible by creating a shell script. In principle, a shell script is nothing more than a text file containing a sequence of commands to be executed by the shell. For example, you may create a shell script file, named dwp, with the following content:

#!/bin/sh
date
who
ps ax
The first line indicates that this is a shell script to be interpreted and executed by Bourne shell (or bash). The rest is the sequence of commands executed by the shell.

To make the above shell script, open your favorite editor and save the file in one of the directories in your $PATH. For example, /usr/local/bin is a good option (on Windows, this directory is equivalent to "C:\cygwin\usr\local\bin" if your cygwin root directory is "C:\cygwin"). You also need to set the "execution bit" of the script file, so that you can "execute" the file simply by typing its name in the command line. You can set its execution bit through the following command (which needs to be executed in the directory where the file is located):
chmod a+x dwp

After setting up all of the above correctly, you can run your script simply by typing dwp:
$ dwp
Tue Jun 12 11:50:23 PDT 2007
cho      tty0         Jun  5 10:52 (:0)
      PID    PPID    PGID     WINPID  TTY  UID    STIME COMMAND
I    5500    2796    5500       4492    0 1003   Jun  5 /usr/bin/bash
     4804    4068    4804       5496    2 1003 11:50:28 /usr/bin/ps

A shell script can also contain comments, whose beginning is marked by the pound (#) sign. A comment can start anywhere on a line and continue until the end of the line. For example, the following script does exactly the same thing as above, but it includes more detailed comments on what each command does.
#!/bin/sh
date    # show the current date
who     # show who is currently logged in
ps ax   # show the list of processes

If needed, you can also refer to the value of a shell variable by prefixing the name of the variable with the dollar sign ($).

#!/bin/sh
echo "Your current PATH:"
echo $PATH

For example, the above script will produce an outout like the following (echo is a command that simply prints out its parameters):

Your current PATH:
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/Program Files/MySQL/MySQL Server 5.0/bin:/cygdrive/c/Program Files/Java/jdk1.6.0/bin

In this tutorial, we have learned that:

  1. Shell scripts are simple text files created with an editor.
  2. Shell scripts should be marked as executeable through the command
    chmod a+x script-file
    
  3. Should be located in your $PATH and
  4. The first line should start with #!/bin/sh if it is a Bournes (or Bash) shell script.

If you want to learn more advanced shell script writing, you may find Steve's Bourne shell scripting tutorial helpful.