shift command in linux(Fedora/Centos/RHEL)

shift command in linux(Fedora/Centos/RHEL) with examples

Shift positional parameters or command line arguments.

Rename the command line arguments $N+1,$N+2 ... to $1,$2 ...

It is assumed to be 1 if N is not provided.

Returns success unless N is negative or greater than $#.

Syntax:

shift
shift n

n is an integer value.

Let us see the shift operation in details with example below,

#!/bin/bash

# total number of command-line arguments
echo "Total command line arguments are: $#"

# $* is used to show only the command line arguments
echo "Only command line arguments are: $*"

echo "First Command Line Argument: $1"

shift 3
echo "First Argument After Shift 3: $1"

shift -1
echo "First Argument After Shift -1: $1"

shift
echo "First Argument After Shift: $1"

Save this content as file name 'shift_example.sh'.

$ . shift_example.sh A B C D E F G

Output:

Total command line arguments are: 7
Only command line arguments are: A B C D E F G
First Command Line Argument: A
First Argument After Shift 3: D
bash: shift: -1: shift count out of range
First Argument After Shift -1: D
First Argument After Shift: E

shift 3: shift3 first command line argument position to D.

shift -1: raises count out of range error.

shift: only shift first command line argument to next argument E.




Python installation

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^