Click Here to buy the full 86-page PDF for only £4.99, $9.99 or €6.99, or the Kindle eBook or Paperback (UK here - more countries and details here)

Check out for regular fun and tips.

10. Variables - Part II

There are a set of variables which are set for you already, and most of these cannot have values assigned to them.
These can contain useful information, which can be used by the script to know about the environment in which it is running.

The first set of variables we will look at are $0 .. $9 and $#.
The variable $0 is the basename of the program as it was called.
$1 .. $9 are the first 9 additional parameters the script was called with.
The variable $@ is all parameters $1 .. whatever. The variable $*, is similar, but does not preserve any whitespace, and quoting, so "File with spaces" becomes "File" "with" "spaces". This is similar to the echo stuff we looked at in A First Script. As a general rule, use $@ and avoid $*.
$# is the number of parameters the script was called with.
Let's take an example script:
echo "I was called with $# parameters"
echo "My name is $0"
echo "My first parameter is $1"
echo "My second parameter is $2"
echo "All parameters are $@"

Let's look at running this code and see the output:

$ /home/steve/
I was called with 0 parameters
My name is /home/steve/
My first parameter is
My second parameter is    
All parameters are 
$ ./ hello world earth
I was called with 3 parameters
My name is ./
My first parameter is hello
My second parameter is world
All parameters are hello world earth
Note that the value of $0 changes depending on how the script was called. The external utility basename can help tidy this up:
echo "My name is `basename $0`"
$# and $1 .. $2 are set automatically by the shell.
We can take more than 9 parameters by using the shift command; look at the script below:
while [ "$#" -gt "0" ]
  echo "\$1 is $1"

This script keeps on using shift until $# is down to zero, at which point the list is empty.

Another special variable is $?. This contains the exit value of the last run command. So the code:

if [ "$?" -ne "0" ]; then
  echo "Sorry, we had a problem there!"

will attempt to run /usr/local/bin/my-command which should exit with a value of zero if all went well, or a nonzero value on failure. We can then handle this by checking the value of $? after calling the command. This helps make scripts robust and more intelligent.
Well-behaved applications should return zero on success. Hence the quote:

One of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C Programs. (Robert Firth)

The other two main variables set for you by the environment are $$ and $!. These are both process numbers.
The $$ variable is the PID (Process IDentifier) of the currently running shell. This can be useful for creating temporary files, such as /tmp/my-script.$$ which is useful if many instances of the script could be run at the same time, and they all need their own temporary files.

The $! variable is the PID of the last run background process. This is useful to keep track of the process as it gets on with its job.

Another interesting variable is IFS. This is the Internal Field Separator. The default value is SPACE TAB NEWLINE, but if you are changing it, it's easier to take a copy, as shown:
echo "Please input three data separated by colons ..."
read x y z
echo "x is $x y is $y z is $z"

This script runs like this:

$ ./
Please input some data separated by colons ...
hello:how are you:today
x is hello y is how are you z is today

It is important when dealing with IFS in particular (but any variable not entirely under your control) to realise that it could contain spaces, newlines and other "uncontrollable" characters. It is therefore a very good idea to use double-quotes around it, ie: old_IFS="$IFS" instead of old_IFS=$IFS.

Click Here to buy the full 86-page PDF for only £4.99, $9.99 or €6.99, or the Kindle eBook or Paperback (UK here - more countries and details here)

Check out for regular fun and tips.
Steve's Bourne / Bash shell scripting tutorial
Share on Twitter Share on Facebook Share on LinkedIn Share on Share on StumbleUpon

Get the Book:

Buy my 600-page Shell Scripting Book...

Or from other Amazon in other countries: USA, Germany, France, Spain, Italy, Canada, Japan, India, Brazil
Or from other retailers

Buy this Tutorial in Paperback or for Kindle:

USA, UK, Germany, France, Spain, Italy
Or Kindle in these countries: Canada, Australia, Japan, India, Brazil, Mexico

Buy this Tutorial as a DRM-Free PDF

(Free Sample)

$9.99 US Dollars

£4.99 UK Pounds

€6.99 Euros

You can join our Shell Scripting community on Facebook: