1 # Check for interactive bash and that we haven't already been sourced. 2 [ -z "$BASH_VERSION" -o -z "$PS1" -o -n "$BASH_COMPLETION" ] && return 3 4 # Check for recent enough version of bash. 5 bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.} 6 if [ $bmajor -gt 3 ] || [ $bmajor -eq 3 -a $bminor -ge 2 ]; then 7 if shopt -q progcomp && [ -r /etc/bash_completion ]; then 8 # Source completion code. 9 . /etc/bash_completion 10 fi 11 fi 12 unset bash bmajor bminor
if [[ $BASH_COMPLETION_DIR != $BASH_COMPLETION_COMPAT_DIR && \ -d $BASH_COMPLETION_DIR && -r $BASH_COMPLETION_DIR && \ -x $BASH_COMPLETION_DIR ]]; then for i in $(LC_ALL=C command ls "$BASH_COMPLETION_DIR"); do i=$BASH_COMPLETION_DIR/$i [[ ${i##*/} != @(*~|*.bak|*.swp|\#*\#|*.dpkg*|*.rpm@(orig|new|save)|Makefile*) \ && -f $i && -r $i ]] && . "$i" done fi unset i
$ ls i* iconv iftop ifupdown info iproute2 iptables
$ compgen -W 'hi hello how world' h hi hello how
$ complete -W 'word1 word2 word3 hello' foo $ foo w<Tab> $ foo word<Tab> word1 word2 word3
$ complete -F _foo foo
$ function _foo() { echo -e "\n" declare -p COMP_WORDS declare -p COMP_CWORD declare -p COMP_LINE declare -p COMP_WORDBREAKS } $ complete -F _foo foo
$ foo b declare -a COMP_WORDS='([0]="foo" [1]="b")' declare -- COMP_CWORD="1" declare -- COMP_LINE="foo b" declare -- COMP_WORDBREAKS="
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}"
local opts="-h --help -f --file -o --output" if [[ ${cur} == -* ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 fi
function _foo() { local cur prev opts COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" opts="-h --help -f --file -o --output" if [[ ${cur} == -* ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) ) return 0 fi }
$ complete -F _foo foo $ foo - -f --file -h --help -o --output
case "${prev}" in -f|--file) COMPREPLY=( $(compgen -o filenames -W "`ls *.sh`" -- ${cur}) ) ;; esac
$ foo --file<Tab> a.sh b.sh c.sh