shift - Linux


The shift command in Linux is used primarily within shell scripts to manipulate the positional parameters passed to a script. Essentially, it “shifts” the positional parameters to the left, thus decreasing the count of positional parameters by one (by default). This command is most effective in handling scripts that take an unspecified number of parameters or when iterating over parameters sequentially.


The basic syntax of the shift command is:

shift [n]


  • n (optional) is the number of positions you want to shift. If not specified, the default is 1.


The shift command has only one option, which is implicitly the numeric argument n:

  • n: Specifies the number of shifts to occur. If n exceeds the number of positional parameters, no error will occur, but all the positional parameters will be unset. This is usable in script loops or conditional structures to iteratively access all parameters.


  1. Basic Usage:

    # Suppose a script is called with ./ one two three
    echo $1  # Prints 'one'
    echo $1  # Now prints 'two'
  2. Specifying a Shift Count:

    # Assume the script was invoked as follows: ./ a b c d e
    echo $1  # Outputs 'a'
    shift 3
    echo $1  # Outputs 'd'
  3. Using in a Loop:

    # Process all arguments
    while [ "$#" -gt 0 ]; do
        echo $1

Common Issues

  • Exceeding Parameter Count: Attempting to shift more positions than there are parameters does not result in an error but will clear out all positional parameters, possibly causing unexpected behavior if not checked.

    Solution: Always compare n with $# (the total number of positional parameters) before shifting by a large number.


shift is commonly used alongside conditional and loop structures to iterate over all arguments in a script. Here’s how it might be combined with other commands:

# A script to process input options
while [ "$#" -gt 0 ]; do
    case "$1" in
            echo "Verbose mode on."
            echo "File set to $file."
            shift 2
            echo "Unknown option: $1"
  • getopts: A utility for parsing positional parameters, often used alongside shift to handle complex options.
  • set: Allows manipulation of shell options and positional parameters.

For further reading and more complex examples, consider checking the official GNU Bash documentation: