为什么在获取文件后 user@hostname 颜色会发生变化?

Bil*_*adj 3 bash prompt

我注意到每当我获取文件时,user@hostname 的颜色都会发生变化:

在此处输入图片说明

为什么?

COi*_*Oil 6

因为它会加载/etc/bash.bashrc文件中指定的用户设置。并且提示设置可能与您实际的终端上下文不同。有关更多详细信息,请查看其他赞成的答案。:)


muc*_*lux 6

这是因为 /etc/bash.bashrc 为您的提示设置了一个新值:

# set a fancy prompt (non-color, overwrite the one in /etc/profile)
# but only if not SUDOing and have SUDO_PS1 set; then assume smart user.
if ! [ -n "${SUDO_USER}" -a -n "${SUDO_PS1}" ]; then
  PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
Run Code Online (Sandbox Code Playgroud)


Per*_*uck 6

提示user@host事物)的外观由变量控制PS1。可能你有类似的东西

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
Run Code Online (Sandbox Code Playgroud)

在您的~/.bashrc文件中。奇怪的\[\033[00m\]等字符用于为提示着色。 \u是用户名 ( h4ck3r),\h是主机名 ( h4ck3rE780),\w是当前工作目录(~表示您的 HOME 目录)。

当你 source 时/etc/bash.bashrc,变量PS1被设置为其他值(没有颜色),你会得到一个不同的提示。这很简单。

尝试

PS1='user=\u, host=\h, directory=\w $'
Run Code Online (Sandbox Code Playgroud)

在终端窗口中查看结果。采购/etc/bash.bashrc没有做任何不同的事情(除了实际参数)。