Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of 'set -- $args' after getopt?

Tags:

bash

getopt

The usual example for using getopt in bash is as follow

args=`getopt abo: $*`
errcode=$?
set -- $args

What does that last line achieve?

like image 203
Maic López Sáenz Avatar asked Dec 06 '25 20:12

Maic López Sáenz


1 Answers

This explains it very well. Essentially, it is to break a single argument with multiple flags into multiple arguments each with single flag:

Whether you call your script as

script -ab

or as

script -a -b

after the set -- $args, $1 will be -a and $2 will be -b. It makes processing easier.

BTW, getopts is much better

like image 63
Miserable Variable Avatar answered Dec 08 '25 12:12

Miserable Variable



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!