24. Special Parameters
Bash supports a lot of special read only parameters. The positional parameters 1, 2, 3, ... are set to the parameters which were given to a Bash script or Shell function:
$ ppars() {
echo $1, $2, $3
}
$ ppars Oranges lemons
Oranges, lemons,@ and * expands each positional parameter to a single word:
$ ppars() {
echo $@
}
$ ppars Oranges lemons
Oranges lemonsWithin double quotes * expands all postional parameters to a single word only, separated by the first character of the variable IFS:
$ ppars() {
IFS="~|-"
echo "$*"
}
$ ppars Oranges lemons
Oranges~lemons"#" expands to the number of positional parameters:
$ pnum() {
echo $#
}
$ pnum Oranges lemons
2"?" expands to exit status the most recently executed foreground pipe:
$ (exit 255)
$ echo $?
255"-" expands to the current option flags set by the set command or given when invoking bash:
$ set -b
$ echo $-
bhimBH"$" expands to the pid of the shell:
echo $$
4936"0" expands to the name of the bash script:
$ echo $0"_" expands initially to the absolute path of the Bash script, otherwise to the last argument of last called command:
$ ls /root /home
$ echo $_
/home