I'm looking at an excerpt of bash and I'm trying to understand what exactly is happening here, particularly in the COMPREPLY
assignment:
case "$cur" in
-*)
COMPREPLY=( $( compgen -W '-a -d -f -l -t -h --aoption --debug \
--file --log --test --help --' -- $cur ) );;
# Generate the completion matches and load them into $COMPREPLY array.
# xx) May add more cases here.
# yy)
# zz)
esac
I understand we're assigning a value to COMPREPLY
here based on the output of compgen
, but what exactly is --
doing in this assignment? Also, why the double semi-colon?
The meaning of
--
really depends on the program you are executing. In this examplecompgen
. Check the documentation of this program, it should be explained there.A common convention that is not necessarily always followed is to treat everything after
--
as arguments, and do not try to parse as options or flags. As a concrete example, consider the case of running the GNUls
command to get a detailed listing of a file named-t
. Runningls -l -t
will treat-t
as an option (order output by time), not as a filename argument. The solution isls -l -- -t
, this wayls
will not try to parse the arguments after--
. This is just a convention and not all programs may follow it. Note also that I wrote GNUls
not justls
, because other implementations may behave differently.compgen
is abash
builtin command, and the man pages say:Since
$cur
is a variable that contains options (starting with-
), the--
is required (as mentioned in the man pages) to make a distinction betweencompgen
options and input to be processed.The following command is turning the option
--d
in--debug
:If you remove the separator
--
, the command throws an error, becausecompgen
doesn't have any option starting with--d
:The
;;
is a separator used in thecase
,esac
statement to terminate in your example the section started with-*)
. Look atbash
man pages forCompound Commands
to get more info.The double dashes end the argument portion in getopt_long. From the manual.
https://linux.die.net/man/3/getopt_long
The double semi colons end a case branch.