9.4 Positional Parameters and Command Line Arguments
9.4.1 Positional ParametersInformation can be passed into a script via the command line. Each word (separated by white space) following the script name is called an argument. Command line arguments can be referenced in scripts with positional parameters; for example, $1 for the first argument, $2 for the second argument, $3 for the third argument, and so on. After $9, curly braces are used to keep the number as one number. For example, positional parameter 10 is referenced as ${10}. The $# variable is used to test for the number of parameters, and $* is used to display all of them. Positional parameters can be set or reset with the set command. When the set command is used, any positional parameters previously set are cleared out. See Table 9.2. Table 9.2. Positional ParametersPositional Parameter | What It References |
---|
$0 | References the name of the script. | $# | Holds the value of the number of positional parameters. | $ | Lists all of the positional parameters. | $@ | Means the same as $, except when enclosed in double quotes. | "$*" | Expands to a single argument (e.g., "$1 $2 $3"). | "$@" | Expands to separate arguments (e.g., "$1" "$2" "$3"). | $1 … ${10} | References individual positional parameters. | Example 9.11
(The Script)
#!/bin/bash#
Scriptname: greetings2
echo "This script is called $0."
1 echo "$0 $1 and $2"
echo "The number of positional parameters is $#"
(The Command Line)
$ chmod +x greetings2
2 $ greetings2
This script is called greetings2.
greetings and
The number of positional paramters is
3 $ greetings2 Tommy
This script is called greetings2.
greetings Tommy and
The number of positional parameters is 1
4 $ greetings2 Tommy Kimberly
This script is called greetings2.
greetings Tommy and Kimberly
The number of positional parameters is 2
Explanation In the script greetings2, positional parameter $0 references the script name, $1 the first command line agreement, and $2 the second command line agreement. The greetings2 script is executed without any arguments passed. The output illustrates that the script is called greetings2 ($0 in the script) and that $1 and $2 were never assigned anything; therefore, their values are null and nothing is printed. This time, one argument is passed, Tommy. Tommy is assigned to positional parameter 1. Two arguments are entered, Tommy and Kimberly. Tommy is assigned to $1 and Kimberly is assigned to $2. 9.4.2 The set Command and Positional ParametersThe set command with arguments resets the positional parameters[a] Once reset, the old parameter list is lost. To unset all of the positional parameters, use set --. $0 is always the name of the script. [a] Remember, without arguments, the set command displays all the variables that have been set for this shell, local and exported. With options, the set command turns on and off shell control options such as -x and -v. Example 9.12
(The Script)#!/bin/bash## Scriptname: args# Script to test command line arguments
1 echo The name of this script is $0.
2 echo The arguments are $.
3 echo The first argument is $1.
4 echo The second argument is $2.
5 echo The number of arguments is $#.
6 oldargs=$
7 set Jake Nicky Scott # reset the positional parameters
8 echo All the positional parameters are $*.
9 echo The number of postional parameters is $#.
10 echo "Good-bye for now, $1."
11 set $(date) # reset the positional parameters
12 echo The date is $2 $3, $6.
13 echo "The value of $oldargs is $oldargs."
14 set $oldargs
15 echo $1 $2 $3
(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 Good-bye for now, Jake.
12 The date is Mar 25, 2000.
13 The value of $oldargs is a b c d.
Explanatioin The name of the script is stored in the $0 variable. $* represents 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). All positional parameters are saved in a variable called oldargs. The set command allows you to reset the positional parameters, clearing out the old list. Now, $1 is Jake, $2 is Nicky, and $3 is Scott. $* represents all of the parameters, Jake, Nicky, and Scott. $# represents the number of parameters, 3. $1 is Jake. After command substitution is performed, i.e., date is executed, the positional parameters are reset to the output of the date command. The new values of $2, $3, and $6 are displayed. The values saved in oldargs are printed. The set command creates positional parameters from the values stored in oldargs. The first three positional parameters are displayed. Example 9.13
(The Script)#!/bin/bash
Scriptname: checker
Script to demonstrate the use of special variable# modifiers and arguments
1 name=${1:?"requires an argument" }
echo Hello $name
(The Command Line)
2 $ checker checker: 1: requires an argument
3 $ checker Sue Hello Sue
Explanation The special variable modifier :? will check whether $1 has a value. If not, the script exits and the message is printed. The program is executed without an argument. $1 is not assigned a value; an error is displayed. The checker program is given a command line argument, Sue. In the script, $1 is assigned Sue. The program continues. How $* and $@ Differ The $* and $@ differ only when enclosed in double quotes. When $* is enclosed within double quotes, the parameter list becomes a single string. When $@ is enclosed within double quotes, each of the parameters is quoted; that is, each word is treated as a separate string. Example 9.14
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 $* is expanded, the quotes surrounding apple pie are stripped; apple and pie become 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 the variable i. The positional parameters are set. By enclosing $* in double quotes, the entire parameter list becomes one string, apple pie pears peaches. The entire list is assigned to i as a single word. The loop makes one iteration. The positional parameters are set. Unquoted, $@ and $* behave the same way (see entry 2 of this explanation). The positional parameters are set. By surrounding $@ with double quotes, each of the positional parameters is treated as a quoted string. The list would be apple pie, pears, and peaches. The desired result is finally achieved.
|