skip to content
 

Writing simple shell scripts

Introduction

At the most basic level, a shell script is just a sequence of shell commands typed into a file so that they can be run with a single command. This page will take you through writing your first shell script.

The shell is a fully-fledged programming language, and if you want to learn more about it you are strongly advised to attend one or more of the UIS's free training courses.

Your first script

The following is a very basic shell script. Save it to a file called hello.sh. It assumes you have a file called todo in your home directory - if not, the script will still run, but will produce an error message at the end.

#!/bin/sh
# This is a trivial shell script.
echo Hello, I am a test shell script.
echo

# Who and where are you?
echo -n "Your userid is "
whoami
echo and you are logged into $HOSTNAME.
echo

# Is anyone else using your machine?
echo Who else is using your computer?
w
echo

# Date and to-do list
echo -n "Today is "
date
echo Your to-do list for today is
cat ~/todo

Some notes:

  • The name you give the script need not be hello.sh, but using the extension .sh serves as a reminder that this is a shell script.
  • The first line, #!/bin/sh, tells the shell that this is a shell script rather than any other type of script.
  • Blank lines and lines beginning with a # sign (comment lines) are ignored by the shell.
  • If you are unfamiliar with any of the commands in the script, type e.g. man whoami (and consider attending the introductory Unix course mentioned above).

Making it executable and running it

To run a script you type its full pathname e.g ./hello.sh if the script is in your current directory, ~/hello.sh if it is in your home directory. (In the next section we'll see how to make the script work when we type hello.sh on its own.)

If you try this now you'll probably get the error message "Permission denied". The problem is that the script is a type of text file and has been created with file permissions appropriate for a text file. You can read it and edit it but not execute it.

To correct this problem, type chmod a+x hello.sh (to let everyone run your script) or chmod u+x hello.sh (to let yourself but no-one else run it) and try running the script again.

Help the shell find it

In Unix, every command is a file somewhere on the system (except for shell builtin commands). Type which command-name to find out where it is. E.g. which date will give the reply /bin/date.

The shell searches the directories on your PATH for any command you type without a full pathname. Type echo $PATH to see the list of directories it searches.

You can't put your own commands in system directories like /bin or /usr/bin, but your own bin directory should be in your PATH (probably listed as /home/username/bin).

If so, just move hello.sh to your bin directory: mv hello.sh ~/bin

The script should now run when you type hello.sh. If not, try closing your terminal window and opening a new one.

If ~/bin is not in your PATH, or if you wish to store your script somewhere else, you will need to change your PATH - read the page on customising your shell and startup files.