Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.