I'm building a bash completion script for a tool which shares file uploading semantics with curl.
With curl, you can do:
curl -F var=@file
to upload a file.
My application has similar semantics and I wish to be able to show possible files after the '@' is pressed. Unfortunately, this is proving difficult:
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [[ "$cur" == @* && "$prev" == '=' ]]; then
COMPREPLY=( $(compgen -f ${cur:1}) )
return 0
fi
So if a command (so far) ends with:
abc=@
Files in the current directory will show.
var=@/usr/
/usr/bin /usr/games
Problem is if I actually hit tab to complete, the '@' goes away!
var=/usr/bin
So it looks like bash replaces the entire current word with the tabbed COMPREPLY.
The only way to avoid this has been to do this:
COMPREPLY=( $(compgen -f ${cur:1}) )
for (( i=0; i<${#COMPREPLY[@]}; i++ ));
do
COMPREPLY[$i]='@'${COMPREPLY[$i]}
done
But now the tab completion looks weird to say the least:
@/usr/bin @/usr/games
Is there anyway to show a normal file tab completion (without the '@' prefix) but preserve the '@' when hitting tab?