Positional Parameters and shift
In this page:
Basic Positional Parameters
$1, $2, and so on refer to the arguments passed to the script (or the current function), while $0 is always the script's own name and $# is the total count of arguments.
Example: Basic Positional Parameters
#!/bin/bash
set -- alpha beta gamma
echo "script/function name placeholder: $0"
echo "first: $1, second: $2, third: $3"
echo "total arguments: $#"
Login to try C/C++/Java/PHP code in the editor
Shifting Through Arguments
shift removes $1 and renumbers every remaining argument down by one, so the old $2 becomes the new $1. This is the standard way to process an arbitrary-length argument list one item at a time.
Example: Shifting Through Arguments
#!/bin/bash
set -- one two three
while [[ $# -gt 0 ]]; do
echo "processing: $1"
shift
done
echo "no arguments left: $#"
Login to try C/C++/Java/PHP code in the editor
Shifting by More Than One
shift N removes the first N positional parameters at once, which is useful when a flag and its value need to be consumed together in a manual argument-parsing loop.
Example: Shifting by More Than One
#!/bin/bash
set -- --name Ada extra1 extra2
if [[ $1 == "--name" ]]; then
value=$2
shift 2
fi
echo "parsed name: $value"
echo "remaining: $*"
Login to try C/C++/Java/PHP code in the editor
Positional Parameters Inside Functions
Functions have their own independent set of positional parameters based on how they were called, completely separate from the script's own $1, $2, etc., even though the same $1 syntax is used.
Example: Positional Parameters Inside Functions
#!/bin/bash
show_args() {
echo "inside function: $1 $2"
}
set -- outer1 outer2
echo "outer script args: $1 $2"
show_args inner1 inner2
echo "outer script args unchanged: $1 $2"
Login to try C/C++/Java/PHP code in the editor
- Forgetting
$0is the script's own name, not the first argument; the first real argument is$1. - Using
$*when"$@"was needed, losing the distinction between separate arguments once word splitting or quoting differs. - Calling
shiftmore times than there are remaining arguments, which is usually harmless but can be a sign of a loop-counting bug.
$0is the script name,$1,$2, ... are the actual arguments, and$#is the argument count.shift(orshift N) discards the first (or first N) positional parameters, renumbering the rest.- A
while [[ $# -gt 0 ]]; do ...; shift; doneloop is a standard way to process an unknown number of arguments one at a time. - Inside a function, positional parameters refer to the function's own arguments, not the script's.
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first:
- getopts for Parsing Command-Line Flags
- Positional Parameters and shift
- Process Substitution in Practice (diff <(...) <(...))
- Arrays of Arguments ("$@" vs "$*" Quoting Difference)
- Nameref Variables (declare -n) for Passing Arrays by Reference
- Heredoc Templating (Generating a Config File from Variables)
- Default/Fallback Argument Handling