BashログインシェルでPS1を変更できないのはなぜですか?

Bashログインシェルで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_COMMANDPS1プロンプトが表示される前に機能が再び変更されます。

おすすめ記事