You can pass information to a shell program using command line arguments, the same as any other HP-UX command. These command line arguments can be used within the shell program. They are stored in variables , accessible within the program.
The command line arguments are stored in variables that show the position of the argument in the command line. That is why these are also called positional parameters. The variables that store command line arguments have names from $0 to $9 . Beginning with the tenth command line argument, the argument number is enclosed in braces. The variable names are shown in Table 10-1.
Variable Name | Description |
---|---|
$0 | Shows value of the command itself (program name) |
$1 | First command line argument |
$2 | Second command line argument |
. | . |
. | . |
. | . |
$9 | Ninth command line argument |
${10} | Tenth command line argument |
$# | Total number of command line arguments |
$* | A space-separated list of command line arguments |
Let's see script-05 , which shows how many command line arguments are provided, a list of all arguments, and the value of the first argument. This program is now shown using the cat command.
$ cat script-05 #!/usr/bin/sh echo "Total number of command line arguments is: $#" echo "These arguments are: $*" echo "The first argument is: " $
When you execute the program with three arguments red , green , and blue , the result is as shown below.
$ ./script-05 red green blue Total number of command line arguments is: 3 These arguments are: red green blue The first argument is: red $
The shift command is used to move the command line arguments one position left. The first argument is lost when you use the shift command. Shifting command line arguments is useful when you perform a similar action to all arguments, one-by-one, without changing the variable name. The shift command throws away the left-most variable (argument number 1) and reassigns values to the remaining variables. The value in $2 moves to $1 , the value in $3 moves to $2 , and so on. Let's modify script-05 into script-06 as shown below using the cat command.
$ cat script-06 #!/usr/bin/sh echo "Total number of command line arguments is: $#" echo "These arguments are: $*" echo "The first argument is: " shift echo "New first argument after shift: " shift echo "First argument after another shift: " $
Now let's execute script-06 with the same three arguments we used with script-05 . You can see from the next result that after every shift, a new value is assigned to $1 . This value is the variable that is just on the right side of $1 (i.e., $2 ).
$ ./script-06 red green blue Total number of command line arguments is: 3 These arguments are: red green blue The first argument is: red New first argument after shift: green First argument after another shift: blue $
During the first shift operation, $1 value is lost forever and can't be recovered by the program. The shift command can also do multiple shift operations in one step. For this you need to supply an argument to the shift command. For example, shift 2 will shift two arguments in one step, such that the old values of $1 and $2 will be lost, the value of $3 will be assigned to $1 , the value of $4 will be assigned to $2 , and so on.
Study BreakUse of Variables in Shell Programs
|
Top |