Taking advantage of the Programmable Tab Completion in Bash, Bash scripts that have subcommands can provide TAB completion in order to discover these subcommands in the following manner:
Gather a list of subcommands dynamically, say in a list
COMMANDS
Use the following code to give completion output
if [[ -n $COMP_LINE ]]; then
line=${COMP_LINE#* }
for c in "${COMMANDS[@]}"; do
[[ ${c:0:${#line}} == "${line,,}" ]] && echo "$c"
done
exit
fi
Explaination:
COMP_LINE
is
an indication or this.COMMANDS
Add complete -C prog prog
to ~/.bashrc
, where prog
is the program that
the above completion code would be a part of.
Related:
Reference:
man bash
Tags:
#literature-note #scripting #tips