|
|
Any shell script you run has access to (inherits) the environment variables accessible to its parent shell. In addition, any arguments you type after the script name on the shell command line are passed to the script as a series of variables.
The following parameters are recognized:
For example, create the following shell script called mytest:
echo There are $# arguments to $0: $* echo first argument: $1 echo second argument: $2 echo third argument: $3 echo here they are again: $@When the file is executed, you will see something like the following:
$ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
$# is expanded to the number of arguments to the script,
while $* and $@ contain the entire argument
list. Individual parameters are accessed via $0, which
contains the name of the script, and variables $1 to
$3 which contain the arguments to the script (from left to
right along the command line).
Although the output from $@ and $* appears to be the same, it may be handled differently, as $@ lists the positional parameters separately rather than concatenating them into a single string. Add the following to the end of mytest:
function how_many { print "$# arguments were supplied." } how_many "$*" how_many "$@"The following appears when you run mytest:
$ mytest foo bar quux
There are 3 arguments to mytest: foo bar quux
first argument: foo
second argument: bar
third argument: quux
here they are again: foo bar quux
1 arguments were supplied.
3 arguments were supplied.