A confusion about ${array[*]} versus ${array[@]} in the context of a Bash completion -
I'm taking a knife when writing a Bash Perfection for the first time, and I'm a bit confused by two methods of bash arrays ( $ {array {@}}
and $ {array [*]}
).
Here is the relevant part of the code (it works, but I want to understand it better):
_switch () {local cur perls local root = $ { PERLBREW_ROOT} - $ HOME / perl5 / perlbrew} COMPREPLY = () cur = $ {COMP_WORDS [COMP_CWORD}} perls = ($ ROOT / perls / perl- *) # Remove all but the last part of the name is perls = ($ { Perls [*] ## * /}) COMPREPLY = ($ (compgen -W "$ {perls [*]} / usr / bin / perl" - $ {cur})}}
< P> Bash: Any element of any element can be referenced using $ {name [subscript]}. Braces need to avoid collision with shell filename extension operator. If the subscript is '@' or '*', the word spreads to all the members of the name array. These subscripts are only different when the word appears in double quotes. If the word is double-quoted, $ {name [*]} extends one word with the value of each array member separated from the first letter of the IFS variable, and extends every element named $ {name [@]} For a different word
Now I suppose that I understand that compgen-W
is a string in which there is a word list of possible options, but not in this context Understand that "$ {name [@]} spreads each element of each name into a different word".
Long story short: $ {array [*]}
works; $ {array does not [@]}
I do not want to know why, and I understand that exactly the extension in $ {array [@]}
.
[*]
-expanded arrays is that < Code> "$ {myarray [@]}" Each element of the lead array is assumed to be a different shell-word, while "$ {myarray [*]}"
Results are separated from spaces with all elements of the array in single shell-word (or the first letter of IFS
). Typically, < Code> [@] behavior Let's assume that you want us to have perls = (perl-one perl-2)
and use ls "$ {perls [*]}"
- which is equivalent to ls-perl-is a pearl-two "
, which will search for a single file named perl-one perl-2
, which is likely to be Is not what you wanted ls is equal to "$ {perls [@]}"
ls "perl-one" perl-two "
, which is more useful than doing something There is a possibility.
The -W
option takes a list of comp-words, but should be in the form of a shell-word with a separate vibrate from the space. Note that the command options always take logic (at least as far as I know) take a single shell-word - otherwise no matter what to say, logic to eliminate options, and regular command logic (/ Other option flags) begin.
In more detail:
perls = (perl-one perl-2) compgen -W "$ {perls [*]} / usr / bin / perl" $ {Cur}
equals compgen -W "perl-one perl-2 / usr / bin / perl" - $ {cur}
, which What you want on the other side,
perls = (perl-one perl-2) compgen -W "$ {perls [@]} / usr / bin / perl" - $ {cur} = / Code> equals compgen -W "perl-one" "perl-2 / usr / bin / perl" - $ {cur}
, which is full nonsense : "Pearl-One" is the only vibration that is connected to the -W flag, and the first real argument - which will take the form of a string to complete the console - "perl-two / usr / Bin / perl ". I hope Colegen can complain that this extra logic ("-" and whatever is in $ $) has been given, but apparently it just ignores them.
Comments
Post a Comment