compgen -W [wordlist] will do command subtitution and parameter expansion in wordlist
Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -g -O2 -fdebug-prefix-map=/build/bash-a6qmCk/bash-5.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wall -Wno-parentheses -Wno-format-security uname output: Linux n16-118-182 5.4.0-100-generic #113-Ubuntu SMP Thu Feb 3 18:43:29 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux Machine Type: x86_64-pc-linux-gnu Bash Version: 5.0 Patch Level: 17 Release Status: release Description: I was develop a bash-completion function which can auto-complete history command. But when I use $ compgen -W "$(fc -l -10)" -- "${COMP_WORDS[1]}" It gave me a weird result. After many debug, I found that compgen will do command subtitution and parameter expansion in word list. It executed every $(...) in bash history so it gave a weird result. For example, this command $ compgen -W '`ls`' will display current dir's content. $ compgen -W '$HOME' will display the HOME dir instead of literal '$HOME' $ compgen -W 'HOME' -- 'H' will get HOME but $ compgen -W '$HOME' -- '$' will get nothing. Repeat-By: compgen -W "$(fc -l -10)" -- "${COMP_WORDS[1]}" compgen -W '`ls`' compgen -W '$HOME' compgen -W '$HOME' -- '$'
Re: compgen -W [wordlist] will do command subtitution and parameter expansion in wordlist
2022年3月3日(木) 22:05 ladyrick via Bug reports for the GNU Bourne Again SHell : > After many debug, I found that compgen will do command subtitution > and parameter expansion in word list. This is actually the documented behavior. For example, the bash-completion project relies on this behavior. To obtain the result you want, we need to write it in the following way (i.e. to pass the literal '$(fc -l -10)' to the compgen builtin): compgen -W '$(fc -l -10)' -- "${COMP_WORDS[1]}"
Fwd: compgen -W [wordlist] will do command subtitution and parameter expansion in wordlist
Basically, you can reply to the bug-bash list instead of directly replying to the individuals. 2022年3月3日(木) 23:32 ladyrick : > Thank you for you reply. > > But your example still doesn't work. > > compgen -W '$(fc -l -10)' > of course passed the literal '$(fc -l -10)' to compgen. But inside compgen, > it is again subtituted. prove: >  It depends on what would be the result that you expect. a) If you expect the words obtained from the expanded result of $(fc -l -10), the first picture you have attached in the previous reply [I attach it again for bug-bash list: 66de9...@7e85d46f.87d12062.jpg] is the expected one. In this case, we can just pass the literal $(fc -l -10) to compgen as I have replied. b) If you expect the lines obtained from the expanded result of $(fc -l -10), you need to set IFS=$'\n'. So, IFS=$'\n' compgen -W '$(fc -l -10)' > If i really need the literal '$(fc -l -1)', for example I want to get '$(fc' > when I type '$(f' > I need to do this: >  c) If you expect the literal $(fc -l -1), you need to quote the word as compgen -W '\$\(fc\ -l\ -1\)' or compgen -W \''$(fc -l -1)'\' or tmp='$(fc -l -1)' compgen -W '"$tmp"' What is the result you actually would like to achieve? -- Koichi
Re: compgen -W [wordlist] will do command subtitution and parameter expansion in wordlist
On 3/3/22 7:32 AM, ladyrick via Bug reports for the GNU Bourne Again SHell wrote: Bash Version: 5.0 Patch Level: 17 Release Status: release Description: I was develop a bash-completion function which can auto-complete history command. But when I use $ compgen -W "$(fc -l -10)" -- "${COMP_WORDS[1]}" It gave me a weird result. After many debug, I found that compgen will do command subtitution and parameter expansion in word list. You might have saved yourself some time by reading the documentation instead of relying on your assumptions. -W wordlist The wordlist is split using the characters in the IFS special variable as delimiters, and each resultant word is expanded. Shell quoting is honored within wordlist, in order to provide a mechanism for the words to contain shell metacharacters or characters in the value of IFS. The possible completions are the members of the resul- tant list which match the word being completed. -- ``The lyf so short, the craft so long to lerne.'' - Chaucer ``Ars longa, vita brevis'' - Hippocrates Chet Ramey, UTech, CWRUc...@case.eduhttp://tiswww.cwru.edu/~chet/