Position parameters can be usedShiftcommand to move left. Like whatShift 3said the original $4 now become $ $ , the original $ $ now becomes $ $, etc., the original $, $, $ discarded, don't move. The shift command with no parameters is equivalent to shift 1.
It's very useful.UnixCommand: Shift。 We know that for positional variables or command-line arguments, the number must be deterministic, or whenShellWhen the program does not know its number, you can assign all parameters together to the variable$*。 If the user requests Shell In the case of not knowing the number of positional variables, you can also handle the parameter one by one one by one, that is, in $2, in $ $ shift command execution pre-variable value in shift command is not available after execution.
Examples are as follows:
# Test 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
shift The order has another important use Bsh defines 9 positional variables, from to $9,9 shift command can access more than 9
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 $ $ $.
From
Linux shell-shift Command Usage (reprint)