使用`sudo -i`时如何在bash中保持着色?

Nee*_*war 6 command-line bash ps1

我已将 PS1 变量更改/etc/bash.bashrc

PS1="\n\[\033[0;37m\]\342\224\214\342\224\200\$(if [[ \$? == 0 ]]; then echo \"[\[\033[0;32m\]\[\033[01;32m\]\342\234\223\[\033[0;37m\]]\342\224\200\"; else echo \"[\[\033[0;32m\]\[\033[01;31m\]\342\234\227\[\033[0;37m\]]\342\224\200\"; fi)[$(if [[ ${EUID} == 0 ]]; then echo '\[\033[0;31m\]\h'; else echo '\[\033[0;33m\]\u\[\033[0;37m\]@\[\033[0;96m\]\h'; fi)\[\033[0;37m\]]\342\224\200[\[\033[0;32m\]\w\[\033[0;37m\]]\n\[\033[0;37m\]\342\224\224\342\224\200\342\224\200\342\225\274 \[\033[0m\]"
Run Code Online (Sandbox Code Playgroud)

它适用于所有命令。但是当我运行sudo -i命令时,PS1 值变为默认值。为了取回配色方案,我必须PS1=<same value>手动运行。知道什么可能导致这种行为吗?

fun*_*er7 6

我打算PS1按照 Alaa Ali 的回答添加,我发现了以下几行:

# 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=no
Run Code Online (Sandbox Code Playgroud)

我已将其更改为force_color_prompt=yes,然后重新exit登录,瞧!颜色!

我在 Ubuntu (disco) 上,希望这会有所帮助。我认为它比 PS1 更好,因为有很多代码依赖于这个变量,我不知道到底在做什么,但有人写了它,我不想浪费他/她的时间:-)


Ala*_*Ali 4

将行添加PS1/root/.bashrc.

颜色恢复为默认值的原因是,当您这样做时sudo -i,它会“模拟”root 登录,然后源(应用)root 的.bashrc,这会将颜色恢复为默认值。因此,将这PS1一行添加到 root.bashrc应该可以解决问题。