The use of shift in the Linux shell

Source: Internet
Author: User

Position parameters can be used shiftcommand to move left. Like what shift 3Indicates that the original $4Now become $1, the original $5Now become $2Wait, the original. $1$2$3Discarded $0Do not move. With no parameters. shiftcommand 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.