Previous Table of Contents Next

Command-Line Input

You can ask users to provide input to a script as part of the command-line argument when the script is run. All three shells use the positional parameter $n to specify as many as nine command-line arguments (for example, $1, $2, $3, and so on). $0 is a legitimate variable, and returns the name of the command. Consider the following Bourne shell script, named tryit:


echo $#
for var in $*
     echo $var

NOTE:  With the Korn shell, you can use ${10}, ${11} . . . notation to recognize more than nine command-line arguments.

You can rewrite the tryit script in the following way to work with any of the three shells. You can use each of these command-line arguments to pass filenames or other information into a shell script from a command line.

NOTE:  For the C shell, you cannot use the echo $# statement in the script. Instead, you can use $#argv to hold the number of command-line arguments.

Instead of using $#argv, which is shown in later examples, echo $0 (which displays the name of the script) is substituted in the following example:

echo $Ø
echo $1
echo $2
echo $3

The resulting screen display lists the command-line arguments, as shown below:

oak% tryit one two three

NOTE:  The $n notation does not return an error if no parameters are provided as part of the command. Users do not need to supply command-line arguments, so you cannot be sure that $n will contain a value unless you check the number of positional parameters using $# for the Bourne and Korn shells.

The C shell provides additional syntax for positional parameters, as shown in Table 16-9.

Table 16-9 C Shell $argv Notation

Notation Description
$#argv Counts the number of command-line arguments.
$* Returns the value of all arguments.
$argv Returns the value of all arguments.
$argv[1-3] Returns the value of arguments 1 through 3.
$0 Returns the command used to run the shell script.
$argv[n] Returns the nth argument.
$argv[$#argv} Returns the last argument.

The shell performs range checking on the $argv[n] syntax, but it does not on the $n syntax. If the shell does not find the word, you get the message Subscript out of range.

NOTE:  $argv[0] is not defined and does not return the name of the script as $0 does.

Table 16-10 shows an example script and the output.

Table 16-10 A Sample $argv Script and Its Output

Notation Description
#!/bin/csh -f oak$ argdemo one two three four
echo Number of args = $#argv Number of args = 4
echo All args= $* All args = one two three four
echo All args = $argv All args = one two three four
echo Args 1-3 = $argv[1-3] Args 1-3 = one two three
echo Name of script file = $0 Name of script file = argdemo
echo Script file\? = $argv[0] Script file ? =
echo Second arg = $argv[2] Second arg = two
echo Last arg = $argv[$#argv] Last arg = four
echo Fifth arg = $5 Fifth arg =
echo Fifth arg = $argv[5] Subscript out of range

Shifting Command-Line Arguments

You can use the shift command, which is built into all three shells, to manipulate the positional parameters in a script. The shift command moves each argument from $1 through $n to the left, changing the previous argument list. The shift command is particularly useful in processing positional parameters in while loops; you can use these commands together to process each positional parameter in turn.

In the following Bourne shell example, each argument is displayed in turn. The shift command shifts the list to the left, removing the leftmost value in the list, as shown in the following script:

while [ $# -ne Ø ]
    echo argument: $1

The following example shows the output of this script:

$ tryit one two three
argument: one
argument: two
argument: three

Because the shift command shifts the positional parameters, you need to use $1 in the script only if you want to display (or process) the command-line arguments. The following example shows how to shift positional parameters from a Bourne shell command line:

$ set a b c d e f g h i
$ while [ $# -gt Ø ]; do
> echo $*
> shift
> done
a b c d e f g h i
b c d e f g h i
c d e f g h i
d e f g h i
e f g h i
f g h i
g h i
h i

Refer to the section "Using while Loops" on page 346 for more information about while loops.

Previous Table of Contents Next