Classic Shell Scripting - Arnold Robbins [70]
esac
shift Shift first argument away (see later in text)
done
$*, $@
Represents all the command-line arguments at once. They can be used to pass the command-line arguments to a program being run by a script or function.
"$*"
Represents all the command-line arguments as a single string. Equivalent to "$1 $2 ...". The first character of $IFS is used as the separator for the different values to create the string. For example:
printf "The arguments were %s\n" "$*"
"$@"
Represents all the command-line arguments as separate, individual strings. Equivalent to "$1" "$2" .... This is the best way to pass the arguments on to another program, since it preserves any whitespace embedded within each argument. For example:
lpr "$@" Print each file
The set command serves a number of purposes. (Full information is provided later in Section 7.9.1.) When invoked without options, it sets the value of the positional parameters, throwing away any previously existing values:
set -- hi there how do you do The - - ends options; "hi" starts new arguments
The shift command "lops off" positional parameters from the list, starting at the left. Upon executing shift, the original value of $1 is gone forever, replaced by the old value of $2. The value of $2, in turn, becomes the old value of $3, and so on. The value of $# is decreased by one. shift takes an optional argument, which is a count of how many arguments to shift off the list. Plain shift is the same as shift 1. Here is an annotated example that ties all of these things together:
$ set -- hello "hi there" greetings
Set new positional parameters
$ echo there are $# total arguments
Print the count
there are 3 total arguments
$ for i in $*
Loop over arguments individually
> do echo i is $i
> done
i is hello Note that embedded whitespace was lost
i is hi
i is there
i is greetings
$ for i in $@
Without quotes, $* and $@ are the same
> do echo i is $i
> done
i is hello
i is hi
i is there
i is greetings
$ for i in "$*"
With quotes, $* is one string
> do echo i is $i
> done
i is hello hi there greetings
$ for i in "$@"
With quotes, $@ preserves exact argument values
> do echo i is $i
> done
i is hello
i is hi there
i is greetings
$ shift
Lop off the first argument
$ echo there are now $# arguments
Prove that it's now gone
there are now 2 arguments
$ for i in "$@"
> do echo i is $i
> done
i is hi there
i is greetings
Special variables
Besides the special variables we've just seen, such as $# and $*, the shell has a number of additional built-in variables. Some also have single-character, nonalphabetic names. Others have names consisting of all uppercase letters.
Table 6-3 lists the variables that are built into the shell and that affect its behavior. All Bourne-style shells have more variables than listed here that either affect interactive use or have other uses when doing shell programming. However, these are what you can rely upon for portable shell programming.
Table 6-3. POSIX built-in shell variables
Variable
Meaning
#
Number of arguments given to current process.
@
Command-line arguments to current process. Inside double quotes, expands to individual arguments.
*
Command-line arguments to current process. Inside double quotes, expands to a single argument.
- (hyphen)
Options given to shell on invocation.
?
Exit status of previous command.
$
Process ID of shell process.
0 (zero)
The name of the shell program.
!
Process ID of last background command. Use this to save process ID numbers for later use with the wait command.
ENV
Used only by interactive shells upon invocation; the value of $ENV is parameter-expanded. The result should be a full pathname for a file to be read and executed at startup. This is an XSI requirement.
HOME
Home (login) directory.
IFS
Internal field separator; i.e., the list of characters that act as word separators. Normally set to space, tab, and newline.
LANG
Default name of current locale;