Bash Scripting
  • scripting list
  • How to Make Bash Script Executable Using Chmod
  • Shell Script to Check Linux Server Health
  • How to Concatenate String Variables in Bash [Join Strings]
  • How to Do Bash Variable Substitution (Parameter Substitution)
  • Bash Parameter Expansion with Cheat Sheet
  • Bash getopts with Examples
  • How to Pass all Arguments in Bash Scripting
  • Bash Function Return Value
  • Bash Loop Through Lines in a File
  • Bash readarray with Examples
  • Bash let with Examples
  • Bash expr with Examples
  • Bash read password
  • Bash for Loop Range Variable
  • Bash Arrays Explained
  • Ways to Find Bash Array Length
  • Bash Split String by Delimiter
  • Bash if Options
  • Bash If Statements for String Comparison
  • Debugging Techniques for Bash Scripts
  • Determining if a Bash String is Empty
  • Bash if Statement with Multiple Conditions
  • Meaning of Shebang in Bash Scripts
  • How to Comment Code in Bash Script
  • How to Read CSV File in Bash
  • Bash Scripting: How to Check if File Exists
  • Bash If Else Statements: Examples and Syntax
  • Bash Scripting: How to Check if Directory Exists
  • Bash eval Command with Examples
  • How to Use Sleep Command in Bash Scripting
  • Bash Associative Arrays with Examples
  • Bash Script for Counting Lines in a File
  • How to Use While Loop in Bash for Efficient Scripting
  • Bash basename Command with Examples
  • How to Create Multiline Strings in Bash
  • How to Use Bash if With && Operator
  • 50 Bash Script Examples to Kickstart Your Learning
  • Case statement in Bash Shell Scripting
  • Trimming White Space in Bash
  • How to Extract Filename from Absolute Path in Bash
  • How to Get Directory Path in Bash
  • Extract Extension from File Path in Bash
  • Extract Filename without Extension from Full Path in Bash
  • Bash for Each File in a Directory
  • Bash for Loop with Array
  • Bash Continue – Using with for Loop
  • Bash Backticks vs Dollar Parentheses $()
  • How to Assign Variable in Bash
  • How to Assign Variable in Bash
  • Bash Division Explained
  • Bash Modulo (Division Remainder)
  • Bash While Read Line by Line
  • Bash shift Command
  • Bash Looping Through Array of Strings
  • Bash read Command with Examples
  • Bash Check Empty Array
  • Using Bash For Loops to Iterate Over a List of Strings
  • Bash Break – Using with For Loop
  • How to Use seq With for Loop in Bash
  • How to Use $@ in Bash Scripting
  • Get the Current Script Directory in Bash
Powered by GitBook
On this page
  • shift Command
  • Bash shift examples
  • Conclusion

Bash shift Command

Does your script accept a variable number of arguments? To easily process each argument, bash has shift command. The command allows you to shift the positional parameters passed to a script.

In this tutorial, we learn about bash shift command with examples.

shift Command

Shift is a commonly used Linux built-in command that comes with Bash. As the name suggests, we use this utility to shift the positional parameters to the left. As we shift left, the first argument gets lost during the process and puts each parameter in a lower position. This is a handy tool to have, to discard the arguments which are not required after they are parsed.

The shift operator shifts the current positional parameters ‘c’ times towards the left. During this process, the current positional parameter is assigned the value of the parameter ‘c’ places ahead of it. If ‘c’ is not specified, it is left shifted by 1. c can be specified as zero as well. If done, the command will execute but the shifting of the arguments will not take place.

Assuming there is a script accepting 10 arguments, a shift 4 will result in the following:

  • Older $1, $2 and $3 are discarded

  • Older $4 becomes $1

  • Older $5 becomes $2

  • Older $5 becomes $3

  • Older $10 becomes $7

Here’s the syntax:

shift <c>

Shift command takes just one non-negative optional argument ‘c’ which determines the number of positions the parameters should be left shifted. If c is passed as 0, no parameters are shifted. c has a default value of 1. c cannot be a negative number.

Bash shift examples

Let's look into some practical examples of bash shift.

Read all arguments

Example:

#!/bin/bashwhile test $# -gt 0do echo "Argument 1 is now $1" shiftdone

Run the script as follows:

./shiftScript1.sh 1 2 3 4 5 6 7 8 9

In this example, we are reading one argument at a time while iterating all the arguments over a while loop and making use of the shift command. The arguments list is treated like a queue. Each shift removes the first argument out and the index of each of the remaining arguments is decremented. We continue to do this until the total count of the arguments($#) runs out.

Shift by a specific number

We can use the shift tool to do a left shift by a non-negative integer.

Example:

#!/bin/bashecho "Current values of parameter 1 and 2: " $1 and $2shift 3echo "After 3 shifts: " $1 and $2shift 0echo "After 0 shift: " $1 and $2

Run the script as follows:

./shiftScript2.sh 1 2 3 4 5 6 7 8 9

In the above example, $1and $2 hold the values 1 and 2 respectively.After 3 shifts, $1 will be storing 4 and $2 will have a value of 5. A shift by zero will result in the variables holding the same value and there won’t be any change.

Using shift with arrays

We can use shift with arrays using the set command. It allows us to set the positional parameters, or to display the names and values of shell variables.

Example:

#!/bin/basharr=("a" "b" "c") #arrayset -- ${arr[@]}while [ $# -gt 0 ] do echo "$1" shiftdone

The variable $@ gives the array of input parameters, and $# returns the size of that array. In the example, the array is supplied to the set command. After that, we are running a while loop till the number of positional parameters which have been set by the set command. In the first iteration we get the first array element. After this we shift which makes the array length decrease by 1. We continue to do so till the count is greater than zero.

Pass the First argument to function

Using the shift utility we can pass the argument to the function.

Example:

#!/bin/bash func() #function to convert to UPPERCASE{ echo $1 | tr [:lower:] [:upper:]} while test $# -gt 0do func() $1 shiftdone

Run the script as follows:

./shiftScript4.sh athens rome paris warsaw berlin lisbon

In this example we have written a function that takes a string as an input and converts it into uppercase. This function is used inside the while loop along with the shift command. In every iteration the argument will be passed to the function which converts it to uppercase and prints it on the screen. Then there will be a left shift. This will continue until all the arguments are processed.

Using shift with getopts

Example:

#!/bin/bashwhile getopts ":a:bc" optdo case $opt in a) echo "$OPTARG" ;; b) echo "$0 [-b] [-a A] [-c] C..."; exit;; c) echo $OPTIND ;; \?) echo "Invalid option: -$OPTARG" >&2 ;; esacdoneshift "$((OPTIND-1))" # shift so that $@, $1, etc. point to the non-option arguments

The script only takes the flags -b and -c, and also accepts -a with an argument. It can accept any number of positional arguments. To access these positional arguments, we make use of shift "$((OPTIND-1))". The OPTIND gives the index of the next command line argument. This shift makes sure that $@ and the other inputs point to the positional arguments and not the option arguments.

After the while loop is over, it is important to run the shift so that it performs the shuffling of the positional parameter. This allows the script to execute correctly by shifting the options away. This ensures the processing of the command line arguments.

Conclusion

  • Bash shift command shifts the positional parameters to the left.

  • This command always discards the previous value of $1

  • We can use shift inside a while loop to read all the arguments one by one.

  • Shift can be even used on arrays.

PreviousBash While Read Line by LineNextBash Looping Through Array of Strings

Last updated 1 year ago

We can read passed to the script using a while loop with the help of the shift command.

The Bash built-in command helps to gracefully retrieve the options and the option arguments from a list of parameters. It is used to break up the options in command lines for easy parsing.

all the arguments
getopts
shift getopts
script output - pass an argument to function using shift
shift array
script output - shift specific number
script output - reading all arguments using shift