Home How do I parse command line arguments in Bash?

# How do I parse command line arguments in Bash?

user6387
1#
user6387 Published in September 21, 2018, 8:03 am

Say, I have a script that gets called with this line:

./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile


or this one:

./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile


What's the accepted way of parsing this such that in each case (or some combination of the two) $v, $f, and $d will all be set to true and $outFile will be equal to /fizz/someOtherFile ?

• For zsh-users there's a great builtin called zparseopts which can do: zparseopts -D -E -M -- d=debug -debug=d And have both -d and --debug in the $debug array echo$+debug[1] will return 0 or 1 if one of those are used. Ref: zsh.org/mla/users/2011/msg00350.html – dezza Aug 2 '16 at 2:13