Skip to content

Instantly share code, notes, and snippets.

@ramn
Last active August 14, 2021 14:56
Show Gist options
  • Save ramn/741f13997c9cd90a4fa6 to your computer and use it in GitHub Desktop.
Save ramn/741f13997c9cd90a4fa6 to your computer and use it in GitHub Desktop.
Bash command completion builder
#!/bin/bash
#
# Build shell command completions
#
function _build_completions {
local current_word
COMPREPLY=()
current_word=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "$($1)" -- $current_word))
}
function _build_completions_for_first_script {
# './first_script display_all_my_commands' outputs all commands
# that can be completed for ./first_script, as a space delimited list.
_build_completions './first_script display_all_my_commands'
}
function _build_completions_for_second_script {
_build_completions './second_script display_all_my_commands'
}
complete -o nospace -F _build_completions_for_first_script './first_script'
complete -o nospace -F _build_completions_for_second_script './second_script'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment