bash completion using ls
By David Jones •
This works from shell:
export files=`ls /home/tests/` complete -o filenames -W "${files}" xtestThis script doesn't :
_xtest () { . local cur local files=`ls /home/tests` COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} #case "$cur" in COMPREPLY=( $(compgen -W "${files}" -- ${cur}) ) # esac return 0 } complete -F _xtest -o filenames xtestWhat is the way using a script for autocomplete from file list of a directory ? I'm using a script because only for 1 script option (-test) is this customized autocomplete required, for other script options (-speed, -define) default autocomplete is O.K.
1 Answer
Declaring a variable and assigning it on one line can mask the return values. The use of $(..) instead of legacy backticks is prefered. Given the lack of context of your code snippit the folowing code is the best I can do with out being able to test it.
_xtest () { local cur local files files=$(ls /home/tests) COMPREPLY=() cur=${COMP_WORDS[COMP_CWORD]} case "$cur" in -*) COMPREPLY=( $( compgen -W $files --$cur ) );; esac return 0 } complete -F _xtest -o filenames xtest