After processing the current $1, make the former $2 become the next $1.
Which builtin should be used?
shift moves positional parameters left; the default shift removes one, while shift 2 removes two.
Detailed explanation
setIncorrect. set replaces positional parameters rather than simply advancing them.
Incorrect. set replaces positional parameters rather than simply advancing them.
unset 1Incorrect. unset cannot remove a positional parameter in this way.
Incorrect. unset cannot remove a positional parameter in this way.
shiftCorrect. shift discards the first parameter and moves the remaining ones left.
Correct. shift discards the first parameter and moves the remaining ones left.
execIncorrect. exec replaces the current process with another command.
Incorrect. exec replaces the current process with another command.
Try it yourself
An example you can run in a temporary verification environment.
bash --noprofile --norc -c 'shift; printf "%s:%s\n" "$1" "$#"' demo first second thirdExpected result
second:2Key points
- Default shift removes one
- $# decreases
- $0 does not change
Notes
- Environment: GNU Bash 5.2 / 短命シェル
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.