Arguments arrive as numbered parameters
When you run a command such as cp a.txt b.txt, the program receives the words as its argument list. A script gets the same list. Inside the script, the parameters have special names. $1 is the first argument, $2 is the second, and so on up to $9. For larger numbers the braces are required, so the tenth argument is ${10}. Writing $10 means the value of $1 followed by a literal zero.
The parameter $0 is not an argument. It holds the name the script was started with, such as hello.sh or /workspace/hello.sh. It is useful in messages that print the program name, and usage messages commonly begin with echo "usage: $0 FILE" for that reason.
Counting and looping over arguments
The parameter $# holds the number of arguments, not counting $0. It is zero when the script runs with no arguments. Use it to check that the caller supplied enough values before the script does any work:
if [ "$#" -lt 2 ]; then
echo "need two arguments" >&2
exit 2
fiTo visit each argument, write "$@" in a for loop. That form expands to every argument as a separate word, and it keeps each argument intact even when it contains spaces. The unquoted form $@ and the forms with $* behave differently, and the next section explains why. Inside a function, $# and the positional parameters describe the function arguments rather than the script arguments. A later module covers functions in detail.
Quoting decides the word count
There are two ways to expand the whole list. "$@" produces one word per argument. "$*" produces a single word in which the arguments are joined with the first character of IFS, which is a space by default. Unquoted $@ and $* both split each argument again at spaces, which breaks arguments that contain spaces. In practice, use "$@" almost every time.
Suppose you run a script with two arguments, one two and three. A loop over "$@" prints [one two] and [three] on two lines. A loop over "$*" prints the joined string [one two three] once, because the quoted form becomes a single word. The difference is easy to see in a test, so run one before you rely on it.
Moving through the list with shift
The shift command removes the first argument. After a shift, $1 holds what used to be $2, $2 holds the old $3, and $# goes down by one. The value of $0 does not change. The form shift N removes N arguments at once. Shifting is useful when a script reads a fixed first argument, such as a sub-command name, and then passes the rest to another command.
Shifting when no arguments remain returns a nonzero status, and bash may print a message. Guard the call when the number of arguments is not known, usually by checking $# first. A loop that consumes the list is a common alternative to the for form:
while [ "$#" -gt 0 ]; do
echo "arg: $1"
shift
doneA common pattern combines the count and the shift. A script that accepts a subcommand first reads that word, shifts it away, and then hands the remaining arguments to a function or another program. Written this way, the first argument selects the branch and the rest of the list is passed on unchanged. Because "$@" keeps each argument whole, the forwarded list arrives exactly as the caller typed it, including any arguments with spaces.
The set builtin can also replace the list. The command set -- a b c sets $1 to a, $2 to b, and $3 to c, and it discards the old values. This is handy for splitting a string into words in a controlled way, although the array features in a later module are usually a better tool for that job.
Defaults for missing arguments
Under the default settings, a missing parameter expands to an empty string. That is not an error, and it often produces a confusing result later. The form ${1:-default} gives a fallback value when $1 is unset or empty. The form ${1:?message} stops the script and prints the message when the value is missing. The second form suits required arguments, because the script fails at the point where the mistake happens.
name=${1:-world}
echo "hello $name"Defaults also keep scripts readable. Instead of a block of if statements that set variables when arguments are missing, one line of expansion states the default in place, next to the place where the value is used. The reader sees the rule at the point of use, and the script has fewer branches to test.
The colon form treats an empty argument the same as a missing one, so a caller who passes an empty string also gets the default. If you need to tell the two apart, drop the colon, as in ${1-world}, which checks only whether the parameter is unset.
Hands-on: inspect the parameters
Create show.sh with the counting and loop code, then run it with several arguments. The commands below show the quoting rule with one argument that contains a space. Run them in the lab terminal and compare the output with the rule you just read.
cat > show.sh << 'EOF'
echo "count=$#"
for arg in "$@"; do
echo "[$arg]"
done
EOF
bash show.sh "one two" threeTry a second run with no arguments, and then one with three simple words. The count changes each time, and the loop body runs once for each argument, or not at all when there are none. Notice that the loop body never needs to know the count. The loop and the count answer different questions, and keeping them separate makes each one easy to read.
The first line of output reports count=2, which is the number of arguments and not the number of words you typed on the command line. The bracketed lines show that one two stayed together as one argument.