Skip to content

Instantly share code, notes, and snippets.

@apchamberlain
Last active December 24, 2015 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apchamberlain/6819201 to your computer and use it in GitHub Desktop.
Save apchamberlain/6819201 to your computer and use it in GitHub Desktop.
bash completion for 'service' command on Linux
# Add bash completion for services: it tries to complete the service you want
# based on the output of "service --status-all"
# Source this as a separate file from your .bash_profile or just copy and paste into it
# Inspired by http://en.newinstance.it/2011/06/30/ssh-bash-completion/
__service_known_services() {
service --status-all |awk '{print $1}'
}
_service() {
local cur known_services
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
known_services="$(__service_known_services)"
if [[ ! ${cur} == -* ]] ; then
COMPREPLY=( $(compgen -W "${known_services}" -- ${cur}) )
return 0
fi
}
complete -o bashdefault -o default -o nospace -F _service service 2>/dev/null \
|| complete -o default -o nospace -F _service service
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment