What is the Purpose of Using shift Command in Shell Scripts?

In the shell script when the user passes the command line arguments, they are stored in the positional parameters $1, $2, $3… variables. The shift command is utilized to switch the positional parameters (Command Line Arguments) to the left. It is beneficial for the user to exclude any unknown arguments or array elements in the list.

In this post, the purpose and usage of the shift command in the shell script will be demonstrated.

Usage of the shift Command in Shell Script

The shift command is utilized in the bash script with the following syntax:

Syntax:

shift n

Use the shift command and define the number “n” to switch to the particular positional parameter.

Example 1: Default Behavior of Shift Command 

The default behavior of using the shift command in a shell script is to switch/move the command line argument one position left. Ultimately, the first argument is lost and the remaining arguments are re-positioned.

The following script justifies the above statements:

#!/bin/bash
echo "Passed Arguments are $@"
shift
echo "Argument After shift $@"
shift
echo "Argument After Another shift $@"

The script is defined as:

  • $@” variable printing the list of passed arguments.
  • shift” operator switches/moves the positional parameters to the left.

Save and exit the above script in the text editor.

Pass the command line arguments to test the above script:

$ bash shift-script.sh 1 2 3 4 5

The above output clearly shows that arguments are omitted one by one.

Example 2: Print Arguments Using Shift Command

In the following script, the shift is used to shift the value of the passed arguments:

#!/bin/bash
# Total Passed Arguments
echo "Total arguments passed are: $#"

# $* is Listing the Passed Arguments
echo "The arguments are: $*"

echo "The First Argument is: $1"
shift 3

echo "The First Argument After Shift 3 is shifted to:  $1"
shift

echo "The First Argument After another Shift is shifted to $1"

The description of the above script is defined below:

  • Firstly, the “$#” variable is used to print the total number of passed arguments.
  • After that, the “$*” variable prints the list of all passed arguments, and the $1 prints the first argument variable via the “echo” command.
  • Furthermore, “shift 3” shifts the $1 variable to point to the third argument.
  • Another “shift” shifts the $1 variable to the next location.
  • Lastly, prints the result with an “echo” statement.

Save and exit the script of the file.

Run the script file by passing the command line argument to test it:

$ bash shift-script.sh 23 45 67 87 45

The total number of passed arguments is 5 (23, 45, 67, 87, and 45), the first argument ($1) is 23 which is shifted to 87 then the last location 45 from the left side.

Example 3: Iterate Values Using Shift Command

The user can also use the shift in a while loop as shown in the following script:

#!/bin/bash

a=1;
b=$#;
while [ $a -le $b ]
do
    echo "Author's Name - $a: $1";
    a=$((a + 1));
    shift 1;
done

The script is defined as below:

  • The “a” variable is initialized with 1 to begin the loop and as well as to print the author’s number.
  • After that, the “b” variable stores the total number of passed arguments to define the ending duration of the loop.
  • Then, the “while” loop initialized with the condition “$a” variable is less equal (le) to the variable “$b”.
  • In the next line, the “do” statement prints the author’s name (passed arguments) $1 variable.
  • Then the “a” variable is incrementing by 1 to continue loop iteration.
  • In the end, the “shift 1” variable is utilized to shift the $1 to the next passed arguments.

Save the above script in the file.

Let’s pass the list of authors “Henry Joseph Milton Timbot” to print in the terminal:

$ bash shift-script.sh Henry Joseph Milton Timbot

The list of authors “Henry”, “Joseph”, “Milton”, and “Timbot” is printed using the “shift” operator.

Conclusion

In the shell script, the purpose of the shift command is to shift/move the positional arguments to the left that set each parameter to the lower position. The default behavior of the “shift” is to shift the parameter to one position left. Users can also specify the number of shifts using the syntax “shift n”, where n is the number of shifts. This write-up has illuminated the purpose of the “shift” command in the shell script with various examples.