Shift command in Linux with examples Last Updated : 27 Sep, 2024 Comments Improve Suggest changes Like Article Like Report 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. Syntaxshift nHere, 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 CommandLet'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 ScriptTo create the shell script, open a terminal and type the following command:vi sampleshift.shNow 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 ScriptNow 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.shStep 3: Execute the Shell Script with Command-Line ArgumentsBut here we have to pass command-line arguments so we can use the following command sh sampleshift.sh G1 G2 G3 G4Here, we are passing 4 command-line arguments named G1, G2, G3, and G4. Below is the screenshot of the output of using shift command: ConclusionMastering 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. Comment More infoAdvertise with us P palakbansal Follow Improve Article Tags : Linux-Unix linux-command Similar Reads SAR command in Linux to monitor system performance sar (System Activity Report) It can be used to monitor Linux system's resources like CPU usage, Memory utilization, I/O devices consumption, Network monitoring, Disk usage, process and thread allocation, battery performance, Plug and play devices, Processor performance, file system and more. Linux s 9 min read How to Securely Copy Files in Linux | scp Command Secure file transfer is a crucial part of Linux systems administration. Whether moving sensitive files between local machines or transferring data between servers, or you need to move backup files to a remote server, fetch logs from a hosted machine, or sync directories across multiple systems, scp 10 min read screen command in Linux with Examples The screen command is an advanced terminal multiplexer that allows you to have multiple sessions within one terminal window. It's like having "tabs" in your Linux terminal â you can open, detach, switch, or resume sessions at any time without losing what you're working on. It's particularly convenie 7 min read script command in Linux with Examples The 'script' command in Linux is a versatile tool that allows you to record all terminal activities, including inputs and outputs, making it a valuable resource for developers, system administrators, educators, and anyone who needs to document terminal sessions. This command captures everything disp 5 min read scriptreplay command in Linux with Examples scriptreplay command is used to replay a typescript/terminal_activity stored in the log file that was recorded by the script command. With the help of timing information, the log files are played and the outputs are generated in the terminal with the same speed the original script was recorded. The 3 min read sdiff command in Linux with Examples sdiff command in Linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays a greater than sign if the line only exists in the file spe 3 min read Sed Command in Linux/Unix With Examples The SED command (short for Stream Editor) is one of the most powerful tools for text processing in Linux and Unix systems. It's commonly used for tasks like search and replace, text transformation, and stream editing.With SED, you can manipulate text files without opening them in an editor. This mak 8 min read select command in Linux with examples select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of commands written in the select block and then asks again to enter a number, if a wrong option is entered it does nothing. If the user enters 2 min read seq command in Linux with Examples The 'seq' command in Linux is a powerful utility used to generate a sequence of numbers. It is particularly useful in scenarios where you need to create a list of numbers within loops, such as while, for, or until loops. With 'seq', you can quickly generate numbers from a starting value (FIRST) to a 4 min read setsid command in Linux with Examples setsid command in Linux system is used to run a program in a new session. The command will call the fork(2) if already a process group leader. Else, it will execute a program in the current process. The main advantage of using 'setsid' is that it allows programs to run independently of the terminal 3 min read Like