[CENTOS] Use bash-completion to type long commands without looking at man or help

1. Are you using the input completion function?

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-1. Prerequisites

  • Linux and MacOS

1-2. TL;DR

  • I used the input completion function, but I didn't know that it could be used for option input of Linux commands. Embarrassing ... </ font>

2. systmctl option length problem

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! ** **

3. What is bash-completion?

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.

3-1. Status of each distribution

Depending on the type and version of your Linux distribution, it may not be included in the standard repository.

  • ** RHEL6 series: ** EPEL repository
  • ** RHEL7 series and later: ** Standard repository
  • ** Ubuntu 18.04 LTS: ** 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.

3-2. Installation on RHEL6 OS

For RHEL 6, CentOS 6, and Oracle Linux 6, bash-completion must be installed from the EPEL repository. The procedure will be described.

  1. Make sure the EPEL repository is enabled. It can be used if the line exists and is enabled as follows.
$ yum repolist all | grep -i epel
epel                         Extra Packages for Enterprise Linux enabled: 12,586
  1. In cases other than the above, enable the EPEL repository depending on the situation.

** 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
  1. When it is displayed in "Available Packages" as shown below, it is not installed, but it exists in the repository.
$ sudo yum list bash-completion
Loaded plugins: refresh-packagekit, security, ulninfo
Available Packages
bash-completion.noarch                          1:1.3-7.el6                          epel
  1. After confirming this, install bash-completion.
$ sudo yum install bash-completion -y
  1. Log out / login to enable bash-completion. The reason for logging out is to read the bash-completion definition file at /etc/profile.d/bash_completion.sh.

3-3. Bash-completion definition file

The definition file of bash-completion exists under the following directory.

  • /etc/bash_completion.d/
  • /usr/share/bash-completion/completions/

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

3-4. Various input completion examples

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/

3-5. Additional modules

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.

  1. The EPEL repository may be enabled by default in the cloud. Check EPEL with the following command. If you see this, you can install it immediately.
$ sudo yum list bash-completion-extras
Loaded plugins: langpacks, ulninfo
Available Packages
bash-completion-extras.noarch          1:2.1-11.el7           ol7_developer_EPEL
  1. If the EPEL repository is not installed, install it.

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
  1. Install the bash-completion-extras package. That's it.

python


$ sudo yum install bash-completion-extras -y
  1. If you check the contents of the package, you can see that there are nearly 300 modules, including mysql and postgresql.
$ 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

4. Summary

  • ** You can use bash-completion to complete command options **
  • ** RHEL6 series needs to be installed from EPEL **
  • ** RHEL7 / 8 and Ubuntu are often installed by default. At least exists in standard repositories **

Recommended Posts