![为什么 Ubuntu 中 root 的 .bashrc 中的 bash 补全设置被注释掉了?](https://linux22.com/image/204487/%E4%B8%BA%E4%BB%80%E4%B9%88%20Ubuntu%20%E4%B8%AD%20root%20%E7%9A%84%20.bashrc%20%E4%B8%AD%E7%9A%84%20bash%20%E8%A1%A5%E5%85%A8%E8%AE%BE%E7%BD%AE%E8%A2%AB%E6%B3%A8%E9%87%8A%E6%8E%89%E4%BA%86%EF%BC%9F.png)
我使用的是 Ubuntu 20.04,我发现在 root 的 .bashrc 文件中,bash 完成设置默认被注释掉:
# enable programmable completion features (you don't need to enable
# this, if it's already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
# if [ -f /etc/bash_completion ] && ! shopt -oq posix; then
# . /etc/bash_completion
# fi
当我取消注释时,补全再次起作用。为什么默认评论了?这是否与任何安全考虑有关?
答案1
原因已在您显示的代码中进行了解释。只需阅读评论(强调我的):
启用可编程完成功能(如果已在 /etc/bash.bashrc 和 /etc/profile 中启用,则无需启用此功能 源 /etc/bash.bashrc)。
由于以下文件,Ubuntu 系统上默认启用完成功能/etc/profile.d/bash_completion.sh
:
$ cat /etc/profile.d/bash_completion.sh
# Check for interactive bash and that we haven't already been sourced.
if [ -n "${BASH_VERSION-}" -a -n "${PS1-}" -a -z "${BASH_COMPLETION_VERSINFO-}" ]; then
# Check for recent enough version of bash.
if [ ${BASH_VERSINFO[0]} -gt 4 ] || \
[ ${BASH_VERSINFO[0]} -eq 4 -a ${BASH_VERSINFO[1]} -ge 1 ]; then
[ -r "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion" ] && \
. "${XDG_CONFIG_HOME:-$HOME/.config}/bash_completion"
if shopt -q progcomp && [ -r /usr/share/bash-completion/bash_completion ]; then
# Source completion code.
. /usr/share/bash-completion/bash_completion
fi
fi
fi
该文件又源自/etc/profile
,这是启动登录 shell 时读取的文件:
$ grep -A6 profile\.d /etc/profile
if [ -d /etc/profile.d ]; then
for i in /etc/profile.d/*.sh; do
if [ -r $i ]; then
. $i
fi
done
unset i
fi
这意味着不需要在 root~/.bashrc
文件中重新启用它,它已经通过上面的文件启用了。事实上,您显示的代码毫无意义,因为/etc/bash_completion
Ubuntu 上的默认内容只是:
$ cat /etc/bash_completion
. /usr/share/bash-completion/bash_completion
这正是/etc/profile.d/bash_completion.sh
.