Shift command in Linux with examples
Shift is a built-in command in bash that after getting executed, shifts/moves the command line arguments to one position left. The first argument is lost after using the shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command line arguments which are not needed after parsing them.
Syntax
shift n
Here, n is the number of positions by which you want to shift command-line arguments to the left if you do not specify, the default value of n is assumed to be 1 i.e. shift works the same as shift 1.
Basic Example of the shift Command
Let's walk through a practical example where we create a shell script named 'sampleshift.sh' that demonstrates the use of the shift command.
Step 1: Create the Shell Script
To create the shell script, open a terminal and type the following command:
vi sampleshift.sh
Now paste the following code:
#!/bin/bash
# total number of command-line arguments
echo "Total arguments passed are: $#"
# $* is used to show the command line arguments
echo "The arguments are: $*"
echo "The First Argument is: $1"
shift 2
echo "The First Argument After Shift 2 is: $1"
shift
echo "The First Argument After Shift is: $1"
Step 2: Save and Exit the Script
Now to save the file press ESC and then type ":x" without quotes and hit Enter. Now to execute the file, use the following command on Linux terminal
sh sampleshift.sh
Step 3: Execute the Shell Script with Command-Line Arguments
But here we have to pass command-line arguments so we can use the following command
sh sampleshift.sh G1 G2 G3 G4
Here, we are passing 4 command-line arguments named G1, G2, G3, and G4. Below is the screenshot of the output of using shift command:

Conclusion
Mastering the shift command in Bash is essential for creating more efficient and streamlined shell scripts. It enables dynamic management and processing of command-line arguments, adding flexibility and power to your scripts. Whether you need to skip arguments or iterate through them, shift is a valuable tool that simplifies complex argument handling.