Position parameters can be used
shift
command to move left. Like what
shift 3
Indicates that the original
$4
Now become
$1
, the original
$5
Now become
$2
Wait, the original.
$1
、
$2
、
$3
Discarded
$0
Do not move. With no parameters.
shift
command is equivalent to
shift 1
。 A very useful Unix command: Shift. We know that for positional variables or command-line arguments, the number must be deterministic, or when the Shell program does not know its number, you can assign all parameters together to the variable $*. If the user requires that the Shell do not know the number of positional variables, but also one by one to the parameter one by one processing, that is, after $ $, after the $ $ $, and so on. The value of the variable before the shift command executes is not available after the shift command executes.
Examples are as follows:
#测试 shift Command (X_SHIFT.SH)
Until [$#-eq 0]
Do
echo "The first parameter is: The number of arguments is: $#"
Shift
Done
Execute the above procedure x_shift.sh:
$./x_shift.sh 1 2 3 4
The results are shown below:
The first parameter is: 1 The number of parameters is: 4
The first parameter is: 2 The number of parameters is: 3
The first parameter is: 3 The number of parameters is: 2
The first parameter is: 4 The number of parameters is: 1
From the above, the shift command executes every time, the number of variables ($#) minus one, and the value of the variable is one bit ahead of the other, the following code calculates all the arguments of the command line with the until and shift commands.
#shift the application of the command on the file (x_shift2.sh)
If [$#-eq 0]
Then
echo "Usage:x_shift2.sh parameter"
Exit 1
Fi
Sum=0
Until [$#-eq 0]
Do
sum= ' expr $sum + $ '
Shift
Done
echo "sum is: $sum"
Execute the above procedure:
$x _shift2.sh 10 20 15
It shows the result as:
45
The shift command has another important purpose, and BSH defines 9 positional variables, from $9 to $9, which does not mean that the user can only use the command line with more than 9 parameters, and with the shift command, it is possible to access more than one or more of them.
The shift command moves the number of arguments at a time, specified by the parameters they take. For example, when the shell program finishes processing the first nine command-line arguments, you can use the Shift 9 command to move the $ $ $.
The use of shift in the Linux shell