$_ CLI In Depth
Learning progress 0%
lesson scripting exit-codes control-flow

Positional Parameters: $1, $@ & shift

6 min read Module 13: Scripting Basics & Exit Codes Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:28

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:

BASH4 lines
if [ "$#" -lt 2 ]; then
  echo "need two arguments" >&2
  exit 2
fi

To 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:

BASH4 lines
while [ "$#" -gt 0 ]; do
  echo "arg: $1"
  shift
done

A 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.

BASH2 lines
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.

BASH7 lines
cat > show.sh << 'EOF'
echo "count=$#"
for arg in "$@"; do
  echo "[$arg]"
done
EOF
bash show.sh "one two" three

Try 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.

Positional parameters and quoting

Visualizer

Arguments arrive as $1, $2 and so on. Whether "$@", "$*" or a bare $@ keeps an argument with a space intact depends only on the quotes.

1 / 5
set -- one "two words" three
one$1
two words$2
three$3
$#
3

set -- replaces the positional parameters. The quotes make two words one argument, so $# is 3.

set -- one "two words" three; printf '[%s]\n' "$@"; echo ---; printf '[%s]\n' "$*"; echo ---; printf '[%s]\n' $@
Terminal lab · 2 steps · real Linux in your browser

Read arguments in a script

Print the argument count and each argument, then use shift and defaults on the list.

01
Print the count and each argument
Create args.sh in /workspace. It prints the number of arguments as count=N. Then it prints each argument on its own line inside square brackets. Each argument must stay whole, even when it contains a space. Test it with a quoted argument that contains a space, and then with no arguments at all.
Show hint for step 1
Use a for loop over the form that keeps each argument as its own word, and quote it. Without the quotes, the two-word argument would split into two arguments.
02
Shift past the first argument
Create greet.sh in /workspace. Its first argument is a name, and the name defaults to world when no argument is given. The script removes that first argument with shift, but only when there is one to remove. It prints hello followed by the name, then prints rest=N, where N is the number of arguments left after the shift. With no arguments it should print hello world and rest=0. With the three names Ana, Ben, and Cy it should print hello Ana and rest=2.
Show hint for step 2
Read the name into a variable first, with a default. Guard the shift with an if test on $#, and print the remaining count last.
Check yourself Question 1 of 4
Concept Check Count after two arguments recall

You run ./show.sh with the arguments one two and three. The first argument contains a space. What is the value of $# inside the script?

Recap & Key Takeaways

The parameters $1 through $9, and ${10} for larger numbers, hold the arguments, $0 holds the script name, and $# holds the argument count.
Use "$@" to pass or loop over arguments, because it keeps each argument whole, while "$*" joins them into one word.
shift removes the first argument and renumbers the rest, and a guard on $# prevents shifting an empty list.
Expansions such as ${1:-default} supply a fallback, and ${1:?message} stops the script when a required argument is missing.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue