shift builtin
The builtin that drops $1 and moves every remaining positional parameter down by one, lowering $# as well.
In more detail
A while loop that handles $1 and then shifts walks through all the arguments. After getopts, shift "$((OPTIND - 1))" removes the options so only the operands remain.
Example
while [ "$#" -gt 0 ]; do echo "$1"; shift; doneRelated terms
- positional parameters — The numbered variables $1, $2 and so on that hold the arguments given to a script or function, with $# holding how many there are.
- getopts — The builtin that parses short command-line options such as -v, -vq and -f value, one option per call, in a while loop.
- OPTIND — The variable getopts uses to track which argument it will read next; after the loop it points at the first operand.
Taught in
- Arguments: Dollar One, Dollar At, shift — Bash Scripting & Command Line Automation
- Option Parsing With getopts — Bash Scripting & Command Line Automation