How To
Summary
There are a number of parameters which the common shells (ksh, bash etc) treat in a special way.
This is not an exhaustive look at them, but may help out in many cases ....
Objective
Environment
Steps
For example
$ testscript one two three
would search the user's PATH and invoke the first program or script called testscript, and the first positional parameter would be one and called $1, the second would be two and called $2 and the last one would three and called $3. $0 is the name of the script itself, in this case, testscript
$# Expands to the number of positional parameters in decimal
$? Expands to the exit status of the most recently executed foreground pipeline (usually 0 for success)
$$ Expands to the process ID of the shell. In a () subshell, it expands to the PID of the current shell, not the subshell.
$! Expands to the process ID of the most recently executed background (asynchronous) command.
$- Expands to the current option flags as specified upon invocation, by the set builtin command,
or those set by the shell itself (such as the -i option).
$* Expands to the positional parameters, starting from $1.
When the expansion occurs within double quotes, it expands to a single word
$@ Expands to the positional parameters, starting from $1.
When the expansion occurs within double quotes, each parameter expands to a separate word.
The difference between $* and $@ is subtle, and indeed, to all intents and purposes, they often behave the same way as each other.
In the case of $*, with the expansion happening between double quotes, the individual parameters are separated by the first character of the IFS shell variable.
The shell variable $IFS is the Internal Field Separator. The usual value for this is a three character sequence of <SPACE><TAB><NEW-LINE> and any of its characters will act as a field separator to the shell unless some form of quoting is in place, usually with single quotes, double quotes or backslashes.
You can see the value of $IFS using this little trick:
peach-gaz:/# echo A"$IFS"B | od -a
0000000 A sp ht lf B lf
0000006
We are echoing $IFS in between an A and a B and then using od -a (Octal dump with named character output) to display the characters.
You can see that between the A and the B, there is a sp or a <SPACE> an ht which is a horizontal <TAB> and an lf which is a line-feed or <NEW-LINE>. The final lf is the <NEW-LINE> at the end of the command.
So, if these special parameters are expanded between double quotes,
$* Expands to "$1c$2c$3c..." where c is the first character of $IFS (usually a <SPACE> ie: "$1 $2 $3..."
$@ Expands to "$1" "$2" "$3" ...
and in our example above:
$ testscript one two three
$* Expands to "one two three"
$@ Expands to "one" "two" "three"
Whether or not the difference is important will depend on how the special parameter is then used.
Additional Information
You can contact me:
- mailto:gaz@uk.ibm.com
- Twitter @power_gaz
Document Location
Worldwide
Was this topic helpful?
Document Information
Modified date:
03 May 2021
UID
ibm11116417