Home » 2014 » December » 28 » Shift command with examples

4:02 PM
Shift command with examples

shift - shift command is used to shift positional parameters

FORMAT
       shift [n]

DESCRIPTION
       The positional parameters can be shifted using this command. Positional parameter 1 shall be assigned the value of parameter (1+n), parameter 2 shall be assigned  the     value of parameter (2+n), and so on.


The parameters represented by the numbers "$#" down to "$#-n+1" shall be unset, and the parameter '#' is updated to reflect the new number of positional parameters.

       The value n shall be an unsigned decimal integer less than or equal to the value of the special parameter '#' . 

If n  is  not  given,  it  is assumed to be 1. If n is 0, the positional and special parameters are not changed.


Examples:

Below is a simple script to understand the use of shift command:

shanky@local:/home/shanky/test:> cat shifttest.sh
 

#!/usr/bin/ksh
print "Param1=$1";
print "Param2=$2";
print "Param3=$3";

print "All parameters before shift:$*"
shift 2; #shifting by two positions

print "Param1=$1";
print "Param2=$2";
print "Param3=$3";
print "All parameters after shift:$*"

We have used "shift 2" to shift the positional parameters by 2 positions. When we execute the script, the output is as below:

shanky@local:/home/shanky/test:> ./shifttest.sh 45 67 89 77
Param1=45
Param2=67
Param3=89
All parameters before shift:45 67 89 77
Param1=89
Param2=77
Param3=
All parameters after shift:89 77

You may notice that the first parameter is containing the value of 3rd positional paremters and so on..

 
 

Category: Open System-Linux | Views: 826 | Added by: shanky | Tags: how to shift positional parameters , shift, shift command in linux, shift positional parameters in linu | Rating: 0.0/0

Related blogs


You may also like to see:


[2015-01-12][Open System-Linux]
W3M : A text based web browser and pager from Linux terminal
[2015-05-25][Open System-Linux]
Password-less Login to Linux/Unix Servers in Putty
[2014-12-22][Open System-Linux]
Key mapping inside VI editor
[2015-06-08][Open System-Linux]
CHAGE command in LINUX
[2014-03-11][Open System-Linux]
STRINGS : a command in linux to read the non-text files

Total comments: 0
avatar