为什么我无法在 bash 登录 shell 中更改 PS1?

为什么我无法在 bash 登录 shell 中更改 PS1?

当我 ssh 进入这个远程系统时,我无法修改PS1.但是,当我通过 ssh 登录时,如果我启动非登录 Bash,那么我就可以修改PS1.这是我的控制台输出:

dev ~ ❯ bash --login
dev ~ ❯ echo $PS1
dev \W ❯
dev ~ ❯ PS1="foobar: "
dev ~ ❯ echo $PS1
dev \W ❯
dev ~ ❯ bash
dev ~ ❯ PS1="foobar: "
foobar: echo $PS1
foobar:
foobar: 

这是相同的输出,但在、、和echo的开头和结尾有语句:~/.bash_profile~/.bash_login~/.profile~/.bashrc

dev ~ ❯ bash --login
bash_profile
bash_login
profile
bashrc
bashrc end
profile end
bash_login end
bash_profile end
dev ~ ❯ echo $PS1
dev \W ❯
dev ~ ❯ PS1="foobar: "
dev ~ ❯ echo $PS1
dev \W ❯
dev ~ ❯ bash
bashrc
bashrc end
dev ~ ❯ PS1="foobar: "
foobar: echo $PS1
foobar:
foobar: 

在系统上,默认值PS1似乎是在以下位置设置的/etc/bash.bashrc

PS1='${ENV:-${ENVIRONMENT:-$(basename HOSTNAME)}} \W ❯ '

该文件似乎源自/etc/profile.

# If PS1 is not set, load bashrc || zshenv or set the prompt
# shellcheck disable=SC1091
if [ "${PS1-}" ]; then
  if [ "${BASH-}" ] && [ "${BASH}" != '/bin/sh' ]; then
    [ -f /etc/bash.bashrc ] && . /etc/bash.bashrc
  # elif [ "${ZSH-}" ] && [ "${ZSH}" != '/bin/sh' ]; then
  #   [ -f /etc/zshenv ] && . /etc/zshenv
  else
    # lightning symbol \342\232\241
    "${IS_ROOT}" && PS1='\[\342\232\241\] ' || PS1='❯ '
  fi
fi

注意:最后我希望能够PS1~/.bashrc.

答案1

在我添加这个答案之前,fra-san 在上面的评论中提到了这一点——功劳归于他。

可能是某些内容${PROMPT_COMMAND}正在设置提示。我可以通过以下方式重现您的问题:

function set_ps1() {
    PS1="hi> "
}

$ PROMPT_COMMAND="set_ps1"
hi> PS1="hello "
hi> 

在这种情况下,当我尝试将 PS1 设置为 时"hello",它会更改它并运行PROMPT_COMMAND。该功能PS1在显示提示之前会变回原样。

答案2

我补充道/home/[username]/.bashrc

unset PROMPT_COMMAND
PS1="[\h:\w] "

这会取消设置PROMPT_COMMAND变量,然后允许我设置PS1.

相关内容