shift Command in Linux



The shift is a Linux command used within shell scripts to manipulate positional parameters by shifting their position to the left. Positional parameters, such as $1, $2, $3, and so on, represent command-line arguments passed to a script.

By using the shift command, you can effectively "discard" the first positional parameter, promoting the subsequent arguments to lower positions. This functionality is particularly useful in scripts that need to process a variable number of command-line arguments or perform iterative operations on them.

Table of Contents

Here is a comprehensive guide to the options available with the Shift command −

Syntax of shift Command

The basic syntax of the shift command is straightforward −

shift [n]

Where n (optional) is the number of positions to shift. If omitted, the default is 1.

When you use shift, the positional parameters are adjusted such that $2 becomes $1, $3 becomes $2, and so on. The previous value of $1 is discarded, and any shifted-out arguments are no longer accessible through positional parameters.

How the shift Command Works?

  • Without an argument (shift): Shifts the positional parameters by one.
  • With an argument (shift n): Shifts the positional parameters by n positions, where n must be a non-negative integer.

Examples of the shift Command in Linux

Let's explore a variety of practical examples to see how the shift command can be applied −

  • Basic Shifting of Positional Parameters
  • Processing All Command-Line Arguments
  • Shifting by Multiple Positions

Basic Shifting of Positional Parameters

Imagine a shell script that processes command-line arguments −

#!/bin/bash
echo "First argument: $1"
shift
echo "Now, the first argument is: $1

If you run the script with ./script.sh arg1 arg2, the output will be −

First argument: arg1
Now, the first argument is: arg2

This example demonstrates how the first argument ($1) is shifted out, and the second argument ($2) becomes the new $1.

Processing All Command-Line Arguments

You can use a loop to process all arguments passed to a script −

#!/bin/bash
while [ "$#" -gt 0 ]; do
echo "Processing argument: $1"
shift
done

Running the script with ./script.sh arg1 arg2 arg3 will output −

Processing argument: arg1
Processing argument: arg2
Processing argument: arg3

In this example, the loop continues until all arguments are processed, and shift moves to the next argument.

Shifting by Multiple Positions

If you want to skip multiple arguments, you can specify the number of positions to shift −

#!/bin/bash
echo "Before shift: $1 $2 $3"
shift 2
echo "After shift: $1

Running the script with ./script.sh arg1 arg2 arg3 arg4 will output −

Before shift: arg1 arg2 arg3
After shift: arg3

Here, shift 2 skips the first two arguments, making $3 the new $1.

Conclusion

The shift command in Linux is a valuable tool for working with positional parameters in shell scripting. By enabling you to adjust the position of command-line arguments, it makes handling dynamic input more straightforward and efficient.

Whether you're processing arguments one by one, skipping specific arguments, or building advanced argument parsers, the shift utility provides the flexibility needed for robust script design. Mastering this command is a step toward creating powerful and adaptable shell scripts for diverse automation tasks.

Advertisements