Skip to main content
The shift command is a Bash shell builtin that reassigns the positional parameters by shifting them to the left. When executed, the value of the positional parameter $n+1 is assigned to $1, $n+2 is assigned to $2, and so forth. This operation permanently discards the first n positional parameters from the current shell execution context and decrements the total parameter count ($#) by n.

Mechanics and State Changes

  • Argument n: An optional non-negative integer dictating the shift offset.
  • Default Behavior: If n is omitted, the command defaults to shift 1. If n is 0, no parameters are modified.
  • Immutability of $0: The special parameter $0 (the script or shell name) is strictly excluded from the shift operation and remains unchanged.
  • Parameter Count ($#): The variable $# is dynamically updated to equal $# - n.
  • Unsetting Parameters: The highest n positional parameters that previously held values are unset.

Exit Status

  • 0 (Success): The shift operation completes successfully, meaning n was less than or equal to $#.
  • >0 (Failure): If n is strictly greater than the current number of positional parameters ($#), the command fails. In this state, no positional parameters are modified, and $# remains unchanged.

State Visualization

Tired of Poor Bash Skills? Fix That With Deep Grasping!Learn More