What is the double dash in this bash script excerpt doing

前端 未结 3 1148
天涯浪人
天涯浪人 2021-01-07 03:26

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         


        
3条回答
  •  抹茶落季
    2021-01-07 04:06

    compgen is a bash builtin command, and the man pages say:

    Unless otherwise noted, each builtin command documented in this section as accepting options preceded by - accepts -- to signify the end of the options.

    Since $cur is a variable that contains options (starting with -), the -- is required (as mentioned in the man pages) to make a distinction between compgen options and input to be processed.

    The following command is turning the option --d in --debug:

    compgen -W '-a -d -f -l -t -h --aoption --debug --file --log --test --help --' -- --d
    --debug
    

    If you remove the separator --, the command throws an error, because compgen doesn't have any option starting with --d:

    compgen -W '-a -d -f -l -t -h --aoption --debug --file --log --test --help --' --d
    -bash: compgen: --: invalid option
    compgen: usage: compgen [-abcdefgjksuv] [-o option]  [-A action] [-G globpat] [-W wordlist]  [-F function] [-C command] [-X filterpat] [-P prefix] [-S suffix] [word]
    

    The ;; is a separator used in the case, esac statement to terminate in your example the section started with -*). Look at bash man pages for Compound Commands to get more info.

提交回复
热议问题