1 # shellcheck shell=bash
2 # systemctl(1) completion -*- shell-script -*-
4 # SPDX-License-Identifier: LGPL-2.1-or-later
6 # This file is part of systemd.
8 # Copyright © 2010 Ran Benita
11 local mode=$1; shift 1
12 SYSTEMD_COLORS=0 systemctl $mode --full --legend=no --no-pager --plain "$@" 2>/dev/null
15 __systemd_properties() {
16 {{LIBEXECDIR}}/systemd --dump-bus-properties
20 local w word=$1; shift
22 [[ $w = "$word" ]] && return
27 __filter_units_by_properties () {
28 local mode=$1 properties=$2; shift 2
33 IFS=$',' read -r -a p < <(echo "Names,$properties")
35 readarray -t props < \
36 <(__systemctl $mode show --property "Names,$properties" -- "${units[@]}")
38 for ((i=0; i < ${#props[*]}; i++)); do
39 if [[ -z ${props[i]} ]]; then
40 if (( count == n )) && [[ -n $names ]]; then
47 if [[ ${props[i]%%=*} == 'Names' ]]; then
52 if (( count == n )) && [[ -n $names ]]; then
58 __get_all_units () { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
59 | { while read -r a b; do echo " $a"; done; }; }
60 __get_non_template_units() { { __systemctl $1 list-unit-files "$2*"; __systemctl $1 list-units --all "$2*"; } \
61 | { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }; }
62 __get_template_names () { __systemctl $1 list-unit-files "$2*" \
63 | { while read -r a b; do [[ $a =~ @\. ]] && echo " ${a%%@.*}@"; done; }; }
64 __get_active_units () { __systemctl $1 list-units "$2*" \
65 | { while read -r a b; do echo " $a"; done; }; }
66 __get_active_services() { __systemctl $1 list-units "$2*.service" \
67 | { while read -r a b; do echo " $a"; done; }; }
69 __get_not_masked_unit_files() {
70 # filter out masked, not-found, or template units.
71 __systemctl $1 list-unit-files --state enabled,enabled-runtime,linked,linked-runtime,static,indirect,disabled,generated,transient "$2*" | \
72 { while read -r a b; do [[ $a =~ @\. ]] || echo " $a"; done; }
75 __get_startable_units () {
76 __filter_units_by_properties $1 ActiveState=inactive,CanStart=yes $(
77 { __get_not_masked_unit_files $1 $2
78 # get inactive template units
79 __systemctl $1 list-units --state inactive,failed "$2*" | \
80 { while read -r a b c; do [[ $b == "loaded" ]] && echo " $a"; done; }
83 __get_restartable_units () {
84 # filter out masked and not-found
85 __filter_units_by_properties $1 CanStart=yes $(
86 { __get_not_masked_unit_files $1 $2
87 __get_active_units $1 $2
91 __get_stoppable_units () {
92 # filter out masked and not-found
94 { __get_not_masked_unit_files $1 $2
95 __get_active_units $1 $2
97 __filter_units_by_properties $1 ActiveState=active,CanStop=yes $units
98 __filter_units_by_properties $1 ActiveState=reloading,CanStop=yes $units
99 __filter_units_by_properties $1 ActiveState=activating,CanStop=yes $units
102 __get_reloadable_units () {
103 # filter out masked and not-found
104 __filter_units_by_properties $1 ActiveState=active,CanReload=yes $(
105 { __get_not_masked_unit_files $1 $2
106 __get_active_units $1 $2
110 __get_failed_units() { __systemctl $1 list-units "$2*" \
111 | while read -r a b c d; do [[ $c == "failed" ]] && echo " $a"; done; }
112 __get_enabled_units() { __systemctl $1 list-unit-files "$2*" \
113 | while read -r a b c ; do [[ $b == "enabled" ]] && echo " $a"; done; }
114 __get_disabled_units() { __systemctl $1 list-unit-files "$2*" \
115 | while read -r a b c ; do [[ $b == "disabled" ]] && echo " $a"; done; }
116 __get_masked_units() { __systemctl $1 list-unit-files "$2*" \
117 | while read -r a b c ; do [[ $b == "masked" ]] && echo " $a"; done; }
118 __get_all_unit_files() { __systemctl $1 list-unit-files "$2*" | while read -r a b; do echo " $a"; done; }
125 done < <(machinectl list --full --max-addresses=0 --no-legend --no-pager 2>/dev/null | sort -u; echo ".host")
129 local cur=${COMP_WORDS[COMP_CWORD]} prev=${COMP_WORDS[COMP_CWORD-1]}
130 local i verb comps mode cur_orig
133 [STANDALONE]='--all -a --reverse --after --before --defaults --force -f --full -l --global
134 --help -h --no-ask-password --no-block --legend=no --no-pager --no-reload --no-wall --now
135 --quiet -q --system --user --version --runtime --recursive -r --firmware-setup
136 --show-types --plain --failed --value --fail --dry-run --wait --no-warn --with-dependencies
137 --show-transaction -T --mkdir --marked --read-only'
138 [ARG]='--host -H --kill-whom --property -p -P --signal -s --type -t --state --job-mode --root
139 --preset-mode -n --lines -o --output -M --machine --message --timestamp --check-inhibitors --what
140 --image --boot-loader-menu --boot-loader-entry --reboot-argument --drop-in'
143 if __contains_word "--user" ${COMP_WORDS[*]}; then
145 elif __contains_word "--global" ${COMP_WORDS[*]}; then
151 if __contains_word "$prev" ${OPTS[ARG]}; then
158 comps=$(__systemctl $mode -t help)
161 comps=$(__systemctl $mode --state=help)
164 comps='fail replace replace-irreversibly isolate
165 ignore-dependencies ignore-requirements flush'
167 --kill-whom|--kill-who)
168 comps='all control main'
171 comps=$(compgen -A directory -- "$cur" )
175 comps=$(compgen -A hostname)
178 comps=$(__systemd_properties)
181 comps='full enable-only disable-only'
184 comps=$( systemctl --output=help 2>/dev/null )
187 comps=$( __get_machines )
190 comps='pretty us µs utc us+utc µs+utc'
196 comps='configuration state cache logs runtime fdstore all'
199 comps=$(compgen -A file -- "$cur")
203 comps=$(systemctl --boot-loader-entry=help 2>/dev/null)
206 COMPREPLY=( $(compgen -W '$comps' -- "$cur") )
210 if [[ "$cur" = -* ]]; then
211 COMPREPLY=( $(compgen -W '${OPTS[*]}' -- "$cur") )
216 [ALL_UNITS]='cat mask'
217 [NONTEMPLATE_UNITS]='is-active is-failed is-enabled status show preset help list-dependencies edit set-property revert'
218 [ENABLED_UNITS]='disable'
219 [DISABLED_UNITS]='enable'
220 [REENABLABLE_UNITS]='reenable'
221 [FAILED_UNITS]='reset-failed'
222 [STARTABLE_UNITS]='start'
223 [STOPPABLE_UNITS]='stop condstop kill try-restart condrestart freeze thaw'
224 [ISOLATABLE_UNITS]='isolate'
225 [RELOADABLE_UNITS]='reload condreload try-reload-or-restart force-reload'
226 [RESTARTABLE_UNITS]='restart reload-or-restart'
227 [TARGET_AND_UNITS]='add-wants add-requires'
228 [MASKED_UNITS]='unmask'
230 [ENVS]='set-environment unset-environment import-environment'
231 [STANDALONE]='daemon-reexec daemon-reload default whoami
232 emergency exit halt hibernate hybrid-sleep sleep
233 suspend-then-hibernate kexec soft-reboot list-jobs list-sockets
234 list-timers list-units list-unit-files poweroff
235 reboot rescue show-environment suspend get-default
236 is-system-running preset-all list-automounts list-paths'
237 [FILE]='link switch-root'
238 [TARGETS]='set-default'
239 [MACHINES]='list-machines'
240 [LOG_LEVEL]='log-level'
241 [LOG_TARGET]='log-target'
242 [SERVICE_LOG_LEVEL]='service-log-level'
243 [SERVICE_LOG_TARGET]='service-log-target'
244 [SERVICE_WATCHDOGS]='service-watchdogs'
245 [MOUNT]='bind mount-image'
248 for ((i=0; i < COMP_CWORD; i++)); do
249 if __contains_word "${COMP_WORDS[i]}" ${VERBS[*]} &&
250 ! __contains_word "${COMP_WORDS[i-1]}" ${OPTS[ARG]}; then
251 verb=${COMP_WORDS[i]}
256 # When trying to match a unit name with certain special characters in its name (i.e
257 # foo\x2dbar:01) they get (un)escaped by bash along the way, thus causing any possible
259 # The following condition solves two cases:
260 # 1) We're trying to complete an already escaped unit name part,
261 # i.e foo\\x2dba. In this case we need to unescape the name, so it
262 # gets properly matched with the systemctl output (i.e. foo\x2dba).
263 # However, we need to keep the original escaped name as well for the
264 # final match, as the completion machinery does the unescaping
266 # 2) We're trying to complete an unescaped (literal) unit name part,
267 # i.e. foo\x2dba. That means we don't have to do the unescaping
268 # required for correct matching with systemctl's output, however,
269 # we need to escape the name for the final match, where the completion
270 # expects the string to be escaped.
272 if [[ $cur =~ '\\' ]]; then
273 cur="$(echo $cur | xargs echo)"
275 cur_orig="$(printf '%q' $cur)"
278 if [[ -z ${verb-} ]]; then
281 elif __contains_word "$verb" ${VERBS[ALL_UNITS]}; then
282 comps=$( __get_all_units $mode "$cur" )
285 elif __contains_word "$verb" ${VERBS[NONTEMPLATE_UNITS]}; then
286 comps=$( __get_non_template_units $mode "$cur" )
289 elif __contains_word "$verb" ${VERBS[ENABLED_UNITS]}; then
290 comps=$( __get_enabled_units $mode "$cur" )
293 elif __contains_word "$verb" ${VERBS[DISABLED_UNITS]}; then
294 comps=$( __get_disabled_units $mode "$cur";
295 __get_template_names $mode "$cur")
298 elif __contains_word "$verb" ${VERBS[REENABLABLE_UNITS]}; then
299 comps=$( __get_disabled_units $mode "$cur";
300 __get_enabled_units $mode "$cur";
301 __get_template_names $mode "$cur")
304 elif __contains_word "$verb" ${VERBS[STARTABLE_UNITS]}; then
305 comps=$( __get_startable_units $mode "$cur" )
308 elif __contains_word "$verb" ${VERBS[RESTARTABLE_UNITS]}; then
309 comps=$( __get_restartable_units $mode "$cur" )
312 elif __contains_word "$verb" ${VERBS[STOPPABLE_UNITS]}; then
313 comps=$( __get_stoppable_units $mode "$cur" )
316 elif __contains_word "$verb" ${VERBS[RELOADABLE_UNITS]}; then
317 comps=$( __get_reloadable_units $mode "$cur" )
320 elif __contains_word "$verb" ${VERBS[ISOLATABLE_UNITS]}; then
321 comps=$( __filter_units_by_properties $mode AllowIsolate=yes \
322 $( __get_non_template_units $mode "$cur" ) )
325 elif __contains_word "$verb" ${VERBS[FAILED_UNITS]}; then
326 comps=$( __get_failed_units $mode "$cur" )
329 elif __contains_word "$verb" ${VERBS[MASKED_UNITS]}; then
330 comps=$( __get_masked_units $mode "$cur" )
333 elif __contains_word "$verb" ${VERBS[TARGET_AND_UNITS]}; then
334 if __contains_word "$prev" ${VERBS[TARGET_AND_UNITS]} \
335 || __contains_word "$prev" ${OPTS[STANDALONE]}; then
336 comps=$( __systemctl $mode list-unit-files --type target --all "$cur*" \
337 | { while read -r a b; do echo " $a"; done; } )
339 comps=$( __get_all_unit_files $mode "$cur" )
343 elif __contains_word "$verb" ${VERBS[STANDALONE]}; then
346 elif __contains_word "$verb" ${VERBS[JOBS]}; then
347 comps=$( __systemctl $mode list-jobs | { while read -r a b; do echo " $a"; done; } )
349 elif [ "$verb" = 'unset-environment' ]; then
350 comps=$( __systemctl $mode show-environment \
351 | while read -r line; do echo " ${line%%=*}"; done )
354 elif [ "$verb" = 'set-environment' ]; then
355 comps=$( __systemctl $mode show-environment \
356 | while read -r line; do echo " ${line%%=*}="; done )
359 elif [ "$verb" = 'import-environment' ]; then
360 COMPREPLY=( $(compgen -A variable -- "$cur_orig") )
363 elif __contains_word "$verb" ${VERBS[FILE]}; then
364 comps=$( compgen -A file -- "$cur" )
367 elif __contains_word "$verb" ${VERBS[TARGETS]}; then
368 comps=$( __systemctl $mode list-unit-files --type target --full --all "$cur*" \
369 | { while read -r a b; do echo " $a"; done; } )
370 elif __contains_word "$verb" ${VERBS[LOG_LEVEL]}; then
371 comps='debug info notice warning err crit alert emerg'
372 elif __contains_word "$verb" ${VERBS[LOG_TARGET]}; then
373 comps='console journal kmsg journal-or-kmsg null'
374 elif __contains_word "$verb" ${VERBS[SERVICE_LOG_LEVEL]}; then
375 if __contains_word "$prev" ${VERBS[SERVICE_LOG_LEVEL]}; then
376 comps=$( __get_all_unit_files $mode "$cur" )
377 elif __contains_word "$prev" debug info notice warning err crit alert emerg; then
380 comps='debug info notice warning err crit alert emerg'
382 elif __contains_word "$verb" ${VERBS[SERVICE_LOG_TARGET]}; then
383 if __contains_word "$prev" ${VERBS[SERVICE_LOG_TARGET]}; then
384 comps=$( __get_all_unit_files $mode "$cur" )
385 elif __contains_word "$prev" console journal kmsg journal-or-kmsg null; then
388 comps='console journal kmsg journal-or-kmsg null'
390 elif __contains_word "$verb" ${VERBS[SERVICE_WATCHDOGS]}; then
392 elif __contains_word "$verb" ${VERBS[MOUNT]}; then
393 if __contains_word "$prev" ${VERBS[MOUNT]}; then
394 comps=$( __get_active_services $mode "$cur" )
395 elif [[ "$prev" =~ .service ]]; then
396 comps=$( compgen -A file -- "$cur" )
403 COMPREPLY=( $(compgen -o filenames -W '$comps' -- "$cur_orig") )
407 complete -F _systemctl systemctl