[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Behavior of 'complete -o filenames' unclear
From: |
Chet Ramey |
Subject: |
Re: [Help-bash] Behavior of 'complete -o filenames' unclear |
Date: |
Wed, 10 Oct 2018 11:36:33 -0400 |
User-agent: |
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 |
On 10/9/18 11:07 AM, Michael Siegel wrote:
> Am 05.10.18 um 03:15 schrieb Chet Ramey:
>> On 10/4/18 11:50 AM, Michael Siegel wrote:
>>
>>> Seems like I'm missing something about how these things work together.
>>
>> Look at it this way. You return a list of words from your completion
>> function and tell readline to treat them as filenames. These filenames
>> are handled in the standard way: if they're not absolute, they are
>> relative to the current directory. These words are all relative pathnames,
>> since your completion function cuts $dbm_dir off the front, so when these
>> relative pathnames are inspected using stat(), the attributes returned are
>> going to depend on whether or not there is a file with that name in the
>> current directory.
>
> Ok, thanks for clarifying that. So, I cannot rely on '-o filenames' to
> do the escaping of shell special characters in the file names I put into
> COMPREPLY, but will have to implement that myself. What would be the
> best way to do this? I was thinking of piping the output of 'compgen' to
> 'sed' (after 'cut').
If you want to prevent readline from quoting filenames that appear in
the current directory by removing `-o filenames', you can use one of
the shell facilities that quotes arguments to add the quotes back: printf's
`%q' format or the new-in-bash-4.4 @Q parameter expansion
> Apropos putting file names into COMPREPLY: After some further discussion
> in #bash on Freenode, I have now sanitized the way this is done:
>
> while IFS=$'\n' read -r line
> do
> COMPREPLY+=("$line")
> done < <(compgen -f -- "$dbm_dir/${COMP_WORDS[COMP_CWORD]}" | \
> cut -d '/' -f 5)
You could avoid the pipe by using something like
"address@hidden/}" to remove the leading directory name.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU address@hidden http://tiswww.cwru.edu/~chet/