variables

shift

Renames positional parameters: $2 becomes $1, $3 becomes $2, and so on, decrementing $#. Optional N shifts by N positions and is common in manual argument parsing loops.

shift [N]

Common flags / operators

Flag / Operator Purpose
N Number of positions to shift (default 1)
exit status Non-zero if N > $# (nothing to shift)
affects $@ $@ and $* update accordingly
no options Only takes the numeric argument

Examples

while (( $# )); do echo "$1"; shift; done

Iterate all positional arguments consumingly.

cmd=$1; shift; "$cmd" "$@"

Pop the first arg as a command, pass rest through.

case $1 in -v|--verbose) verbose=1; shift;; esac

Consume a flag in a hand-rolled arg parser.

shift 3

Drop the first three positional parameters.

Gotcha

'shift' inside a function shifts the FUNCTION's parameters, not the script's. Shifting more than $# leaves parameters unchanged and returns non-zero — check the status if it matters.

Related

← All Bash builtins · Linux commands