我使用的是 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
.