Basic Linux Tutorial
Computer Tutorial

Special Shell Variables — # and *



Special Shell Variables — # and *

The special shell variables # and * will provide you with a lot of flexibility when dealing with a variable argument list. You will always know how many arguments have been entered through $#, and you can always access the entire argument list through $*, regardless of the number of arguments. Notice that the command ($0) is never included in the argument list variable $*.

#   The number of command line arguments
*   The entire argument string

Example of a shell script for Special Shell Variables.

#!/usr/bin/sh

echo   " There are $# command line arguments "
echo   " They are $* "
echo   " The first command line argument is $1 "
$ chmod + x color4
$ color4 red green yellow blue
echo   " There are 4 command line arguments "
echo   " They are red green yellow blue "
echo   " The first command line argument is red "
$