Shell--shift usage

Source: Internet
Author: User

Shift is a very useful command in UNIX. You can move the command parameter to the left so that the command parameter position in the script is not changed, and all parameters are traversed in turn.
As Shift 3 indicates that the original $4 now become $ $, the original $ $ now becomes $ $, and so on, the original $, $, $ discard, $ not move. The shift command with no parameters is equivalent to shift 1.

Example: shift_test.sh

#!/bin/SHif[$#-eq0]then Echo"Usage: $ arg1 arg2 ..."Exit1Fiuntil [$#-eq0 ] DoEcho"$*"Echo"First ARG $; Total $#"Shiftdone

Execution Result: $>./shift_test.sh1 2 3 4 51 2 3 4 5First Arg1; Total52 3 4 5First Arg2; Total43 4 5First Arg3; Total34 5First Arg4; Total25First Arg5; Total1

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 $ $ $.

Shell--shift usage

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.