尽管在 .bashrc 中设置了 PS1,但仍无法获得彩色提示

尽管在 .bashrc 中设置了 PS1,但仍无法获得彩色提示

我通过 ssh 登录的其中一台机器没有给我彩色提示,尽管通过 进行了设置.bashrc.bashrc有问题的部分是

# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# uncomment for a colored prompt, if the terminal has the capability; turned
# off by default to not distract the user: the focus in a terminal window
# should be on the output of commands, not on the prompt
force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
        # We have color support; assume it's compliant with Ecma-48
        # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
        # a case would tend to support setf rather than setaf.)
        color_prompt=yes
    else
        color_prompt=
    fi
fi

if [ "$color_prompt" = yes ]; then
    if [ $(id -u) -eq 0 ];
    then # you are root, make the prompt red
        PS1='${debian_chroot:+($debian_chroot)}\[\e[00;33m\]\u\[\e[00m\]@\[\e[00;34m\]\h\[\e[00m\]:\[\e[00;36m\]\w\[\e[00m\]\e[01;31m#\e[00m '
    else
        PS1='${debian_chroot:+($debian_chroot)}\[\e[00;32m\]\u\[\e[00m\]@\[\e[00;34m\]\h\[\e[00m\]:\[\e[00;36m\]\w\[\e[00m\]$ '
    fi
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi
unset color_prompt force_color_prompt

并且我在其他几台机器上使用它并获得了预期的结果。

我不知道为什么这台电脑不使用它.bashrc。如果我使用

export PS1='${debian_chroot:+($debian_chroot)}\[\e[00;32m\]\u\[\e[00m\]@\[\e[00;34m\]\h\[\e[00m\]:\[\e[00;36m\]\w\[\e[00m\]$ '

(上面的那行),我的提示得到了所需的格式。/etc/bash.bashrc存在,而且/etc/profile,两者对我来说看起来都不错。

此外,如果我通过 ssh 进入这台计算机,xterm 标题不会设置。通常,它会为这台机器设置为 user@host。我怀疑根本原因相同,但我不知道该去哪里查找。

答案1

使用 ssh 时,您将获得登录 shell。

~/.profile

# ~/.profile: executed by the command interpreter for login shells.
# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login exists.

因此,请确保此文件存在于您尝试登录的用户的主目录中。如果不存在,则创建该文件并~/.bashrc通过键入以下内容强制其读取该文件 -

if [ -n "$BASH_VERSION" ]; then
    # include .bashrc if it exists
    if [ -f "$HOME/.bashrc" ]; then
        . "$HOME/.bashrc"
    fi
fi

相关内容