12.4. Positional Parameters and Command-Line Arguments
Command-line arguments can be referenced in scripts with positional parameters; for example, $1 is set to the first argument, $2 to the second argument, and $3 to the third argument. Positional parameters can be reset with the set command. See Table 12.5.
Table 12.5. Positional ParametersVariable | Function |
---|
$0 | References the name of the script | $# | Holds the value of the number of positional parameters | $* | Contains a list of all the positional parameters | $@ | Means the same as $*, except when enclosed in double quotes | "$*" | Expands to a single argument, for example, "$1 $2 $3" | "$@" | Expands to separate arguments, for example, "$1" "$2" "$3" |
12.4.1 The set Command and Positional Parameters
The set command sets the positional parameters. If the positional parameters have already been set, the set command will reset them, removing any values in the old list. To unset all of the positional parameters, use set ––.
Example 12.12.
(The Script)
$ cat args
#!/bin/ksh
# Script to test command-line arguments
1 print The name of this script is $0.
2 print The arguments are $*.
3 print The first argument is $1.
4 print The second argument is $2.
5 print The number of arguments is $#.
6 oldparameters=$*
7 set Jake Nicky Scott
8 print All the positional parameters are $*.
9 print The number of positional parameters is $#.
10 print $oldparameters
11 set --
12 print Good–bye for now, $1.
13 set $oldparameters
14 print $*
(The Output)
$ args a b c d
1 The name of this script is args.
2 The arguments are a b c d.
3 The first argument is a.
4 The second argument is b.
5 The number of arguments is 4.
8 All the positional parameters are Jake Nicky Scott.
9 The number of positional parameters is 3.
10 a b c d
12 Good-bye for now ,
14 a b c d
$
EXPLANATION
The name of the script is stored in the $0 variable. $* (and $@) both represent all of the positional parameters. $1 represents the first positional parameter (command-line argument). $2 represents the second positional parameter. $# is the total number of positional parameters (command-line arguments). The variable oldparameters is assigned all of the positional parameters ($*). Later on, if you want to get back your original parameters, you can do so by typing set $oldparameters. Reset positional parameters with the set command. The set command completely clears all previously set parameters. Jake is assigned to $1, Nicky is assigned to $2, and Scott is assigned to $3. The new positional parameters are printed. The number of positional parameters is printed. The original parameters were stored in the variable oldparameters. They are printed. All parameters are unassigned. $1 has no value. The parameters list was cleared with the set –– command. A new parameter list is assigned by substituting the values in oldparameters to the parameter list with the set command. All the positional parameters are printed.
Example 12.13.
(How $* and $@ Differ)
1 $ set 'apple pie' pears peaches
2 $ for i in $*
> do
> echo $i
> done
apple
pie
pears
peaches
3 $ set 'apple pie' pears peaches
4 $ for i in "$*"
> do
> echo $i
> done
apple pie pears peaches
5 $ set 'apple pie' pears peaches
6 $ for i in $@
> do
> echo $i
> done
apple
pie
pears
peaches
7 $ set 'apple pie' pears peaches
8 $ for i in "$@" # At last!!
> do
> echo $i
> done
apple pie
pears
peaches
EXPLANATION
The positional parameters are set. When the $* is expanded, the quotes are stripped and apple pie becomes two separate words. The for loop assigns each of the words, in turn, to the variable i and then prints the value of i. Each time through the loop, the word on the left is shifted off, and the next word is assigned to i. If $* is surrounded by double quotes, all of the words in the list become one single string, and the whole string is assigned to the variable i. The positional parameters are set. By enclosing $* in double quotes, the entire parameter list becomes one string. The positional parameters are set. Unquoted, the $@ behaves the same way as the $*. The positional parameters are set. By surrounding $@ with double quotes, each of the positional parameters is treated as a quoted string. The list would consist of "apple pie", "pears", and "peaches". Each of the quoted words is assigned to i, in turn, as the loop goes through each iteration.
|