Example of a simple shell script

These are the contents of a shell script called display:

   cat display
   # This script displays the date, time, username and
   # current directory.
   echo "Date and time is:"
   date
   echo
   echo "Your username is: `whoami` \n"
   echo "Your current directory is: \c"
   pwd

The first two lines beginning with a hash (#) are comments and are not interpreted by the shell. Use comments to document your shell script; you will be surprised how easy it is to forget what your own programs do!

The backquotes (`) around the command whoami illustrate the use of command substitution.

The \n is an option of the echo command that tells the shell to add an extra carriage return at the end of the line. The \c tells the shell to stay on the same line. See the man page for details of other options.

The argument to the echo command is quoted to prevent the shell interpreting these commands as though they had been escaped with the \ (backslash) character.


[Home] [Search] [Index] This site maintained by webmaster@deslab.mit.edu