bash script parameters

on

When running a bash script a list of arguments can be typed after the command. When a script is invoked the arguments will be assigned to positional parameters. Besides these positional parameters there exist special parameters. I created a script (param.sh) which demo’s all kind of parameters.

knilluz@serv:~> ./param.sh a b c d

expand to the name of the shell script:
$0: ./param.sh

expand the first parameter
$1: a

expand the second parameter
$2: b

expand the third parameter
$3: c

expand all positional parameters (starting from 1)
$@: a b c d

when $@ occurs within double quotes (“$@”), the expansion is treated as a collection of individual quoted strings.
“$@”: a b c d

loop through this “$@” collection
a
b
c
d

expand all positional parameters (starting from 1). (same as $@)
$*: a b c d

when $* occurs within double quotes (“$*”), the expansion is treated as a single word.
“$*”: a b c d

loop through this “$*” string
a b c d

expand to the number of positional parameters (arguments) on the command line
$#: 4

expand to the process ID of the most recently executed background program
$!:

expand to the process ID (PID) of the shell
$$: 9190

expand to the exit status code of the most recently executed foreground program
$?: 0