bashでtabキー補完
bashを使用しているとコマンドを補完してくれます。
下記の通りです。
systemcまで打ち、tabを入力すると残りのtlを補完します。
でも、startなどのサブコマンドはどうでしょうか。
ベルマークで出ていて、コマンドを受け付けないことがわかります。(わかりづらいですが)
今回はこれを解消する方法について検証してみます。
参考
LinuxでTab補完ができない方へ!bash-completionをインストールしよう!
検証してみた
上記参考ページによると下記の流れのようです。
手順①.bash-completionのインストール
手順②.補完機能を読み込む
再起動もしくは再ログイン(bashrcファイルはシェルの起動時に実行されるコマンドが設定されているファイルのためらしい)
使用するOS:OracleLinux8.8 (別の調べ物でこれを使用)
[root@ip-10-0-0-57 ~]# cat /etc/os-release
NAME="Oracle Linux Server"
VERSION="8.8"
ID="ol"
ID_LIKE="fedora"
VARIANT="Server"
VARIANT_ID="server"
VERSION_ID="8.8"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Oracle Linux Server 8.8"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:oracle:linux:8:8:server"
HOME_URL="https://linux.oracle.com/"
BUG_REPORT_URL="https://github.com/oracle/oracle-linux"
ORACLE_BUGZILLA_PRODUCT="Oracle Linux 8"
ORACLE_BUGZILLA_PRODUCT_VERSION=8.8
ORACLE_SUPPORT_PRODUCT="Oracle Linux"
ORACLE_SUPPORT_PRODUCT_VERSION=8.8
[root@ip-10-0-0-57 ~]#
現状のbashrc
[root@ip-10-0-0-57 ~]# cat /etc/bashrc
# /etc/bashrc
# System wide functions and aliases
# Environment stuff goes in /etc/profile
# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.
# Prevent doublesourcing
if [ -z "$BASHRCSOURCED" ]; then
BASHRCSOURCED="Y"
# are we an interactive shell?
if [ "$PS1" ]; then
if [ -z "$PROMPT_COMMAND" ]; then
case $TERM in
xterm*|vte*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
else
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"'
fi
;;
screen*)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/\~}"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
fi
# Turn on parallel history
shopt -s histappend
history -a
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
# You might want to have e.g. tty in prompt (e.g. more virtual machines)
# and console windows
# If you want to do so, just add e.g.
# if [ "$PS1" ]; then
# PS1="[\u@\h:\l \W]\\$ "
# fi
# to your custom modification shell script in /etc/profile.d/ directory
fi
if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it gets undefined at the end of /etc/profile
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
}
# By default, we want umask to get set. This sets it for non-login shell.
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`/usr/bin/id -gn`" = "`/usr/bin/id -un`" ]; then
umask 002
else
umask 022
fi
SHELL=/bin/bash
# Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
if [ "$PS1" ]; then
. "$i"
else
. "$i" >/dev/null
fi
fi
done
unset i
unset -f pathmunge
fi
fi
# vim:ts=4:sw=4
[root@ip-10-0-0-57 ~]#
. /usr/share/bash-completion/bash_completion
の行のコメントアウトを外すとありますが、なんということか存在しないので、追記しようかと思ったけどもググったら下記が出てきました。
How to add bash auto completion in RHEL 8 (Red Hat Linux)
インストーラーが/etc/profile.d/配下にbash_completion.sh を配置してくれるのか。。。
[root@ip-10-0-0-57 ~]# ll /etc/profile.d/
total 72
-rw-r--r--. 1 root root 196 Mar 15 2019 colorgrep.csh
-rw-r--r--. 1 root root 201 Mar 15 2019 colorgrep.sh
-rw-r--r--. 1 root root 1741 Apr 3 2023 colorls.csh
-rw-r--r--. 1 root root 1606 Apr 3 2023 colorls.sh
-rw-r--r-- 1 root root 162 Jun 13 2022 colorxzgrep.csh
-rw-r--r-- 1 root root 183 Jun 13 2022 colorxzgrep.sh
-rw-r--r-- 1 root root 216 Apr 26 2022 colorzgrep.csh
-rw-r--r-- 1 root root 220 Apr 26 2022 colorzgrep.sh
-rw-r--r--. 1 root root 80 Mar 31 2023 csh.local
-rw-r--r-- 1 root root 1107 Dec 15 2017 gawk.csh
-rw-r--r-- 1 root root 757 Dec 15 2017 gawk.sh
-rw-r--r-- 1 root root 2489 Mar 31 2023 lang.csh
-rw-r--r-- 1 root root 2312 Mar 31 2023 lang.sh
-rw-r--r--. 1 root root 500 Mar 7 2019 less.csh
-rw-r--r--. 1 root root 253 Mar 7 2019 less.sh
-rw-r--r--. 1 root root 81 Mar 31 2023 sh.local
-rw-r--r-- 1 root root 120 Oct 2 2022 which2.csh
-rw-r--r-- 1 root root 540 Oct 2 2022 which2.sh
[root@ip-10-0-0-57 ~]#
[root@ip-10-0-0-57 ~]#
インストールする
[root@ip-10-0-0-57 ~]# yum search bash-completion
Last metadata expiration check: 2:37:54 ago on Tue 17 Oct 2023 07:28:56 PM JST.
================================================================================= Name Exactly Matched: bash-completion ==================================================================================
bash-completion.noarch : Programmable completion for Bash
bash-completion.src : Programmable completion for Bash
===================================================================================== Name Matched: bash-completion ======================================================================================
libguestfs-bash-completion.noarch : Bash tab-completion scripts for libguestfs tools
libnbd-bash-completion.noarch : Bash tab-completion for libnbd
libvirt-bash-completion.x86_64 : Bash completion script
nbdkit-bash-completion.noarch : Bash tab-completion for nbdkit
virt-v2v-bash-completion.noarch : Bash tab-completion for virt-v2v
[root@ip-10-0-0-57 ~]#
[root@ip-10-0-0-57 ~]# yum install bash-completion
Last metadata expiration check: 2:38:18 ago on Tue 17 Oct 2023 07:28:56 PM JST.
Dependencies resolved.
==========================================================================================================================================================================================================
Package Architecture Version Repository Size
==========================================================================================================================================================================================================
Installing:
bash-completion noarch 1:2.7-5.el8 ol8_baseos_latest 274 k
Transaction Summary
==========================================================================================================================================================================================================
Install 1 Package
Total download size: 274 k
Installed size: 895 k
Is this ok [y/N]: y
Downloading Packages:
bash-completion-2.7-5.el8.noarch.rpm 1.6 MB/s | 274 kB 00:00
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 1.6 MB/s | 274 kB 00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : bash-completion-1:2.7-5.el8.noarch 1/1
Verifying : bash-completion-1:2.7-5.el8.noarch 1/1
Installed:
bash-completion-1:2.7-5.el8.noarch
Complete!
[root@ip-10-0-0-57 ~]#
[root@ip-10-0-0-57 ~]# ll /etc/profile.d/
total 76
-rw-r--r-- 1 root root 664 Mar 7 2019 bash_completion.sh
-rw-r--r--. 1 root root 196 Mar 15 2019 colorgrep.csh
-rw-r--r--. 1 root root 201 Mar 15 2019 colorgrep.sh
-rw-r--r--. 1 root root 1741 Apr 3 2023 colorls.csh
-rw-r--r--. 1 root root 1606 Apr 3 2023 colorls.sh
-rw-r--r-- 1 root root 162 Jun 13 2022 colorxzgrep.csh
-rw-r--r-- 1 root root 183 Jun 13 2022 colorxzgrep.sh
-rw-r--r-- 1 root root 216 Apr 26 2022 colorzgrep.csh
-rw-r--r-- 1 root root 220 Apr 26 2022 colorzgrep.sh
-rw-r--r--. 1 root root 80 Mar 31 2023 csh.local
-rw-r--r-- 1 root root 1107 Dec 15 2017 gawk.csh
-rw-r--r-- 1 root root 757 Dec 15 2017 gawk.sh
-rw-r--r-- 1 root root 2489 Mar 31 2023 lang.csh
-rw-r--r-- 1 root root 2312 Mar 31 2023 lang.sh
-rw-r--r--. 1 root root 500 Mar 7 2019 less.csh
-rw-r--r--. 1 root root 253 Mar 7 2019 less.sh
-rw-r--r--. 1 root root 81 Mar 31 2023 sh.local
-rw-r--r-- 1 root root 120 Oct 2 2022 which2.csh
-rw-r--r-- 1 root root 540 Oct 2 2022 which2.sh
[root@ip-10-0-0-57 ~]#
bash_completion.shが配置されましたね。
現在のセッションから有効にする方法も記載してくれている
[root@ip-10-0-0-57 ~]# source /etc/profile.d/bash_completion.sh
[root@ip-10-0-0-57 ~]#
参考資料まとめ
LinuxでTab補完ができない方へ!bash-completionをインストールしよう!
How to add bash auto completion in RHEL 8 (Red Hat Linux)