Many shells and command prompts, including ** bash **, provide ** input completion ** (completion). It's a feature that works on Windows, and most people will use it.
Example of file name completion by input completion function
$ ls
sample.txt test1.txt test2.txt test3.txt
$ cat s[TAB]Key input
$ cat sample.txt ★ Press the TAB key to complete the matching file name
** And from around RHEL6 (2010s-), it can be used not only for file names and directory names, but also for command option input. ** </ font>
1-2. TL;DR
I have been using RHEL7 series for the past few years, but I have been using RHEL5 / RHEL6 series for a long time. The confusing thing about RHEL7 is that you can't use the service
command or the chkconfig
command (some of them can be used with compatible functions).
Since it is a command that is often used in system construction and operation, it tends to be "What was chkconfig --list
, systemctl
...".
** The command option input completion function is useful in such cases. ** **
You can see a list of options by typing the "TAB key" after systemctl
as follows:
$ sudo systemctl [TAB]Key input ★ TAB after inserting 1 space after systemctl
add-requires hybrid-sleep reload-or-restart
add-wants is-active reload-or-try-restart
cancel is-enabled rescue
cat is-failed reset-failed
condreload isolate restart
condrestart is-system-running set-default
condstop kexec set-environment
daemon-reexec kill set-property
daemon-reload link show
default list-dependencies show-environment
delete list-jobs snapshot
disable list-sockets start
edit list-timers status
emergency list-unit-files stop
enable list-units suspend
exit mask switch-root
force-reload poweroff try-restart
get-default preset unmask
halt reboot unset-environment
help reenable
hibernate reload
If the list is displayed, this is the one. Then enter list-unit-files
. Input completion of "TAB key" is effective even while inputting options.
$ sudo systemctl list-unit-files ★list-unit-TAB completion works even in the middle of files
** Now I'm not afraid of systemctl
! Hmm! ** **
Bash-completion complements command options rather than completing filenames and directory names. If you type complete -p
and a lot of lines are displayed, you can expect it to be available.
Depending on the type and version of your Linux distribution, it may not be included in the standard repository.
RHEL8 series
$ rpm -q bash-completion
bash-completion-2.7-5.el8.noarch
RHEL7 series
$ rpm -q bash-completion
bash-completion-2.1-6.el7.noarch
RHEL6 series
$ rpm -q bash-completion
package bash-completion is not installed
Ubuntu18.04LTS
$ dpkg -l bash-completion
||/ Name Version Architecture Description
+++-==============-============-============-=================================
ii bash-completio 1:2.8-1ubunt all programmable completion for the b
It's a bit old, so it's subtle whether it's true now, but here's Mac OS information.
For RHEL 6, CentOS 6, and Oracle Linux 6, bash-completion must be installed from the EPEL repository. The procedure will be described.
$ yum repolist all | grep -i epel
epel Extra Packages for Enterprise Linux enabled: 12,586
** When there is a line but disabled **
$ sudo yum-config-mangaer --enable epel
** When there is no line itself **
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm -y
$ sudo yum list bash-completion
Loaded plugins: refresh-packagekit, security, ulninfo
Available Packages
bash-completion.noarch 1:1.3-7.el6 epel
$ sudo yum install bash-completion -y
/etc/profile.d/bash_completion.sh
.The definition file of bash-completion exists under the following directory.
If you check the directory, you will find a large number of definition files. Commands with this file will be able to complete optional input.
$ ls /usr/share/bash-completion/completions/ | wc -l
454
$ ls /usr/share/bash-completion/completions/
addpart fsck.minix lvextend raw timedatectl
blkdiscard fsfreeze lvm readprofile tuned-adm
blkid fstrim lvmdiskscan rename udevadm
blockdev gapplication lvreduce renice ul
bootctl gdbus lvremove repquota umount
busctl getopt lvrename resizepart unshare
bzip2 groupadd lvresize rev useradd
cal groupdel lvs rpm userdel
cfdisk groupmod lvscan rtcwake usermod
★ Omitted below
The previous systemctl
example complemented the list-unit-files
option. Other options that start with a hyphen (-
) or hyphen (-
) can be completed.
$ sudo systemctl -[TAB]Key input
-a --ignore-dependencies --quiet
--after --kill-who -r
--all -l --recursive
--before --no-ask-password --reverse
--defaults --no-block --root
-f --no-legend --runtime
--fail --no-pager -s
--failed --no-reload --signal
--force --now --state
--full --no-wall --system
--global -p -t
-h -P --type
-H --privileged --version
--help --property
--host -q
It will take some time to display, but if you enter the "TAB key" in the middle of the service name (unit file name), you can display a list of matching services.
$ sudo systemctl restart sshd[TAB]Key input
sshd@ sshd.service
sshd-keygen.service sshd.socket
** For ssh **
With ssh, you can display host name candidates from ~ / .ssh / known_hosts
, ~ / .ssh / config
, and / etc / hosts
.
After one space[TAB]Key
$ ssh [TAB]Key input
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.5.1.35 ol7srv.dbprivatenrt01s.work01nrtvcn.oraclevcn.com ol7srv
In the middle of the host name[TAB]Key
$ ssh ol7[TAB]Key input
ol7ksplice
ol7srv
ol7srv2
ol7srv3
ol7srv-vip.dbprivatenrt01s.work01nrtvcn.oraclevcn.com
-o After option[TAB]Key
$ ssh -o [TAB]Key input
AddressFamily= IdentitiesOnly=
BatchMode= IdentityFile=
BindAddress= IPQoS=
ChallengeResponseAuthentication= KbdInteractive
** Everything comes here. I want to try the "TAB key" for the time being. ** **
You can see what you can do with each command by parsing the definition file. However, it is nonsense to remember because there are so many.
It's worth remembering that the command is supposed to specify options like this, but it's not defined when a file or directory in the current directory is displayed.
$ sudo yum-config-managar [TAB]Key input
bash-completion-extras-2.1-11.el7.noarch.rpm
.bash_history
.bash_logout
.bash_profile
.bashrc
.cache/
.config/
.lesshst
sample.txt
.ssh/
bash-completion has an additional module, bash-completion-extras
. This requires an EPEL repository even for RHEL7 series.
Currently, only EPEL7 is available, not EPEL6 and EPEL8. However, even if the suffix of el7 is attached, it is noarch, so if you get it with wget or yumdownloader and install it on RHEL6 / 8 series, you can use it.
$ sudo yum list bash-completion-extras
Loaded plugins: langpacks, ulninfo
Available Packages
bash-completion-extras.noarch 1:2.1-11.el7 ol7_developer_EPEL
RHEL7 series
$ sudo yum install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm -y
AmazonLinux2
$ sudo amazon-linux-extras install epel -y
python
$ sudo yum install bash-completion-extras -y
$ rpm -ql bash-completion-extras |wc -l
271
$ rpm -ql bash-completion-extras
/usr/share/bash-completion/completions
/usr/share/bash-completion/completions/a2x
/usr/share/bash-completion/completions/aclocal
/usr/share/bash-completion/completions/add_members
/usr/share/bash-completion/completions/alias
/usr/share/bash-completion/completions/alternatives
/usr/share/bash-completion/completions/animate
/usr/share/bash-completion/completions/ant
/usr/share/bash-completion/completions/apropos
/usr/share/bash-completion/completions/arch
/usr/share/bash-completion/completions/arping
★ Omitted below