0% found this document useful (0 votes)
18 views3 pages

Shift Command in Linux With Examples

The document explains the 'shift' command in bash, which shifts command line arguments to the left, losing the first argument. It provides syntax, an example shell script, and instructions on how to create and execute the script with command-line arguments. The default shift value is 1, but it can be specified as an integer argument.

Uploaded by

balwindersingh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Shift Command in Linux With Examples

The document explains the 'shift' command in bash, which shifts command line arguments to the left, losing the first argument. It provides syntax, an example shell script, and instructions on how to create and execute the script with command-line arguments. The default shift value is 1, but it can be specified as an integer argument.

Uploaded by

balwindersingh
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

hello ()

{
echo "hegllo "
}
h1()
{
echo "hegllo "
}

h2()
{
echo "this is best in class"
}
echo "this is best in clASS"
h2
hello
h1
Shift command in Linux with examples
 Last Updated : 21 Apr, 2020
Shift is a builtin command in bash which after getting executed,
shifts/move the command line arguments to one position left. The first
argument is lost after using 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.
Example: Let’s create a shell script file named as [Link] as
follows. The total number of command-line arguments is represented
by $#. Use the following command to create the desired shell script
file
vi [Link]
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"


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 [Link]
But here we have to pass command-line arguments so we can use the
following command
sh [Link] 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:

You might also like