shift

2025-01-05

What does shift do?

The shift command is used to rearrange positional parameters within a shell script. Positional parameters are the arguments passed to a script or function. They are accessed using the special variables $1, $2, $3, and so on, where $1 represents the first argument, $2 the second, and so forth. $0 represents the name of the script itself.

shift’s core function is to move each positional parameter one position to the left. This effectively discards the first argument ($1) and reindexes the remaining arguments. For example: if your script receives arguments “apple”, “banana”, “cherry”, after a shift, “banana” becomes $1, “cherry” becomes $2, and “apple” is lost.

Basic Usage of shift

Let’s illustrate with a simple example. Create a file named shift_example.sh with the following content:

#!/bin/bash

echo "Original arguments:"
echo "Argument 1: $1"
echo "Argument 2: $2"
echo "Argument 3: $3"

shift

echo "Arguments after shift:"
echo "Argument 1: $1"
echo "Argument 2: $2"
echo "Argument 3: $3"

Make it executable: chmod +x shift_example.sh

Now run it with some arguments: ./shift_example.sh apple banana cherry

The output will demonstrate how shift rearranges the arguments:

Original arguments:
Argument 1: apple
Argument 2: banana
Argument 3: cherry
Arguments after shift:
Argument 1: banana
Argument 2: cherry
Argument 3: 

Iterating Through Arguments with shift

shift becomes particularly useful when you need to iterate through a variable number of arguments. Consider a script that prints all its arguments:

#!/bin/bash

while [ -n "$1" ]; do
  echo "Argument: $1"
  shift
done

This script continues to loop and print arguments until there are no more arguments left (-n "$1" checks if $1 is not empty). Each iteration, shift removes the current $1 and moves the remaining arguments to the left.

shift with a Number

You can also specify a numeric argument to shift to shift multiple positions at once. shift 2 will discard the first two arguments and reindex the rest.

#!/bin/bash

echo "Argument 1: $1"
echo "Argument 2: $2"
echo "Argument 3: $3"
echo "Argument 4: $4"

shift 2

echo "Arguments after shift 2:"
echo "Argument 1: $1"
echo "Argument 2: $2"
echo "Argument 3: $3"

Running this with four arguments will show how shift 2 removes the first two.

Handling Options with shift

shift is frequently used in conjunction with getopt or other parsing techniques to process command-line options. After handling options, shift can remove them, leaving only the remaining positional arguments. This improves script readability and organization. An example showcasing this would require a more in-depth explanation of option parsing and is beyond the scope of this introductory post.

Advanced Scenarios & Considerations

While the basic functionality of shift is straightforward, its application in complex scripts, especially those involving option parsing, becomes more nuanced. Understanding how shift interacts with other shell features and constructs is essential for advanced usage.