Post by Peng YuI currently use getopt to support -s --long like options in bash scripts.
https://github.com/karelzak/util-linux/blob/master/misc-utils/getopt-parse.bash
I would not advise doing this.
Oh, you need *reasons*? OK, here's the top one: the getopt(1) software
you are using is a nonstandard GNU command which acts ENTIRELY DIFFERENTLY
from the getopt(1) command that is commonly present on Unix systems.
Anyone attempting to run your script on a Unix-that-is-not-GNU system
will encounter the (broken) traditional behavior. Not only will your
--long-options NOT work, but any argument containing a space, or any
empty argument, will just completely break.
From the HP-UX (11.11) getopt(1) man page:
WARNINGS
getopt option arguments must not be null strings nor contain embedded
blanks.
From the OpenBSD getopt(1) at <https://man.openbsd.org/getopt>:
CAVEATS
Note that the construction set -- `getopt optstring $*` is not
recommended, as the exit value from set will prevent the exit value
from getopt from being determined.
BUGS
Whatever getopt(3) has.
Arguments containing whitespace or embedded shell metacharacters
generally will not survive intact; this looks easy to fix but isn't.
The error message for an invalid option is identified as coming from
getopt rather than from the shell procedure containing the invocation
of getopt; this again is hard to fix.
The precise best way to use the set command to set the arguments without
disrupting the value(s) of shell options varies from one shell version
to another.
Post by Peng YuBut this solution does not support complex expressions made of '-a'
'-o' '!' '(' and ')' as in defined in GNU find.
Does anybody know a solution that can make a bash script support these
complex expressions?
Yes: write your own option processing code.
Here's another general tidbit of advice: if you find yourself wondering
how to construct an abstract syntax tree in a bash script, you probably
shouldn't be writing this project in bash.