Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell shift procedure - What is this?

Tags:

linux

shell

shift

In shell we have the command shift, but i saw on some example its giving shift 3

Why there is a number after shift ? and what its about ? what it does ?

Example:

echo “arg1= $1  arg2=$2 arg3=$3”
shift
echo “arg1= $1  arg2=$2 arg3=$3”
shift   
echo “arg1= $1  arg2=$2 arg3=$3”
shift  
echo “arg1= $1  arg2=$2 arg3=$3”
shift

The output will be:

arg1= 1 arg2=2  arg3=3 
arg1= 2 arg2=3  arg3= 
arg1= 3 arg2=   arg3=
arg1=   arg2=   arg3=

But when i add that, it doesn't display it correctly.

like image 853
user1253622 Avatar asked May 02 '12 13:05

user1253622


People also ask

What is a shell procedure?

The shell may be used to read and execute commands contained in a file. For example, sh file [ args ... ] calls the shell to read commands from file. Such a file is called a command procedure or shell procedure.

What is shift in shell?

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.

What is the purpose of the shift command?

The shift command changes the values of the batch parameters %0 through %9 by copying each parameter into the previous one—the value of %1 is copied to %0, the value of %2 is copied to %1, and so on. This is useful for writing a batch file that performs the same operation on any number of parameters.

What is a shell script file?

A shell script is a text file that contains a sequence of commands for a UNIX-based operating system. It is called a shell script because it combines a sequence of commands, that would otherwise have to be typed into the keyboard one at a time, into a single script.


3 Answers

Take a look at the man page, which says:

shift [n]
    The  positional parameters from n+1 ... are renamed to $1 .... 
    If n is not given, it is assumed to be 1.

An Example script:

#!/bin/bash
echo "Input: $@"
shift 3
echo "After shift: $@"

Run it:

$ myscript.sh one two three four five six

Input: one two three four five six
After shift: four five six

This shows that after shifting by 3, $1=four, $2=five and $3=six.

like image 173
dogbane Avatar answered Sep 28 '22 00:09

dogbane


you use man bash to find the shift builtin command:

shift [n]

The positional parameters from n+1 ... are renamed to $1 .... Parameters represented by the numbers $# down to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is 0, no parameters are changed. If n is not given, it is assumed to be 1. If n is greater than $#, the positional parameters are not changed. The return status is greater than zero if n is greater than $# or less than zero; otherwise 0.

like image 35
Zava Avatar answered Sep 28 '22 02:09

Zava


This would be answered simply by reading either the Bash manual, or typing man shift:

      shift [n]

Shift the positional parameters to the left by n. The positional parameters from n+1 ... $# are renamed to $1 ... $#-n. Parameters represented by the numbers $# to $#-n+1 are unset. n must be a non-negative number less than or equal to $#. If n is zero or greater than $#, the positional parameters are not changed. If n is not supplied, it is assumed to be 1. The return status is zero unless n is greater than $# or less than zero, non-zero otherwise.

like image 22
Oliver Charlesworth Avatar answered Sep 28 '22 02:09

Oliver Charlesworth