OnWorks Linux and Windows Online WorkStations

Logo

Free Hosting Online for WorkStations

< Previous | Contents | Next >

Handling Positional Parameters En Masse

It is sometimes useful to manage all the positional parameters as a group. For example, we might want to write a “wrapper” around another program. This means that we create a script or shell function that simplifies the invocation of another program. The wrapper, in this case, supplies a list of arcane command line options and then passes a list of argu- ments to the lower-level program.

The shell provides two special parameters for this purpose. They both expand into the complete list of positional parameters, but differ in rather subtle ways. They are:


Table 32-1: The * And @ Special Parameters


Parameter Description

Parameter Description

$* Expands into the list of positional parameters, starting with 1. When surrounded by double quotes, it expands into a double quoted string containing all of the positional parameters, each separated by the first character of the IFS shell variable (by default a space character).


image

$@ Expands into the list of positional parameters, starting with 1. When surrounded by double quotes, it expands each positional parameter into a separate word surrounded by double quotes.


image


Here is a script that shows these special paramaters in action:

Handling Positional Parameters En Masse


#!/bin/bash

# posit-params3: script to demonstrate $* and $@ print_params () {

echo "\$1 = $1" echo "\$2 = $2" echo "\$3 = $3" echo "\$4 = $4"

}


pass_params () {

echo -e "\n" '$* :'; print_params $* echo -e "\n" '"$*" :'; print_params "$*" echo -e "\n" '$@ :'; print_params $@ echo -e "\n" '"$@" :'; print_params "$@"

}


pass_params "word" "words with spaces"

#!/bin/bash

# posit-params3: script to demonstrate $* and $@ print_params () {

echo "\$1 = $1" echo "\$2 = $2" echo "\$3 = $3" echo "\$4 = $4"

}


pass_params () {

echo -e "\n" '$* :'; print_params $* echo -e "\n" '"$*" :'; print_params "$*" echo -e "\n" '$@ :'; print_params $@ echo -e "\n" '"$@" :'; print_params "$@"

}


pass_params "word" "words with spaces"


In this rather convoluted program, we create two arguments: “word” and “words with spaces”, and pass them to the pass_params function. That function, in turn, passes them on to the print_params function, using each of the four methods available with the special parameters $* and $@. When executed, the script reveals the differences:


image

[me@linuxbox ~]$ posit-param3


$* :


$1

=

word

$2

=

words

$3

=

with

$4

=

spaces

"$*" :


$1

=

word words with spaces

$2

=

$3

=

$4

=

$@ :


$1

=

word

$2

=

words

$3

=

with

$4

=

spaces

"$@" :

$1 = word


$2 = words with spaces

$3 =

$4 =

$2 = words with spaces

$3 =

$4 =


With our arguments, both $* and $@ produce a four word result:

word words with spaces

"$*" produces a one word result:

"word words with spaces"

"$@" produces a two word result:

"word" "words with spaces"

which matches our actual intent. The lesson to take from this is that even though the shell provides four different ways of getting the list of positional parameters, "$@" is by far the most useful for most situations, because it preserves the integrity of each positional parameter.


Top OS Cloud Computing at OnWorks: