Difference between getopt and getopts in shell scripts

Source: Internet
Author: User
Getop string1 string2 .. the characters marked in nstring1 indicate valid signs (-can be followed by characters). If there are: After the flag, it indicates that this sign is followed (there can be 0-N spaces) stringx is its parameter. getop processes string2 .... n, output result-flag 1 parameter 1-flag 2 parameter 2 ..... -- parameter n

40:/tmp/V> getopt "A: B" "-bafile"
-B-a file --

As you can see, getopt is also the rule of getopts to re-combine parameters.
At this level, they are exactly the same
Note that the difference between them is that the shell that provides getopts has the built-in optarg variable, and getopts has modified this variable.
Getopt cannot. Therefore, getopt must use set to reset the location parameter $1, $2 ....
Then, shift is used in getopt to obtain the data in sequence.

 

Furthermore

ARGs = 'getopt Abo: $ *'
Set -- $ ARGs

Note that if a parameter contains spaces, the parameter is changed to multiple parameters. Therefore, if the parameter may contain spaces, getopts must be used. Otherwise, there is no difference between them.
Of course, what's important is
Getopt is an external binary file, while getopts is built-in

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.