【小ネタ】CloudShell でプロンプトを変える方法

~/.bash_profile に環境変数 PS1 を定義するコード追記をします。CloudShell も bash の設定ファイルの読み込み順は Linux とほぼ同一の作法となっています。
2023.07.18

こんにちは、猫とアポロチョコと Systems Manager が好きな、テクニカルサポートの m.hayakawa です。

CloudShell 使っていますか?

EC2 を起動せずに、マネジメントコンソールから直接シェルが起動できて、ログイン中の IAM ユーザー(またはIAMロール)の権限を用いて、AWS CLI コマンドも実行できる優れものです。

デフォルトではプロンプトは、ユーザー名@IPアドレス ディレクトリ$ となっています。

これを変えたくなったので、今回、小ネタとしてブログ記事にします。

始めに結論

結論から書くと、~/.bash_profile に環境変数 PS1 を定義するコード追記をします。ユーザー名、時間、ディレクトリを表示したい場合は、下記のように指定してください。

if [ "$PS1" ]; then
   PS1="[\u@\t \w]\$ "
fi

私は検証時にコマンド実行時刻を記録したいため、このようにしてみました。

[cloudshell-user@02:22:45 ~]$

CloudShell が bash ファイルを読み込む順番について

初めて起動するときには、このような画面になります。

man bash コマンドで仕様を確認します。

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.

これによると、/etc/profile を読み込んだあと、 ~/.bash_profile が存在していれば、それを読み込んで実行するという仕様になっているようです。

デフォルトでは、プロンプト環境変数 PS1 はこのようになっていました。

[cloudshell-user@ip-10-4-50-6 ~]$ echo $PS1                                                                       
[\u@\h \W]\$

この環境変数はどこに定義されているかをチェックしたところ、/etc/bashrc に記載がありました。

bash.rc より一部抜粋

# /etc/bashrc

# System wide functions and aliases
# Environment stuff goes in /etc/profile

# It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates.

# are we an interactive shell?
if [ "$PS1" ]; then
  if [ -z "$PROMPT_COMMAND" ]; then
    case $TERM in
    xterm*|vte*)
      if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
      elif [ "${VTE_VERSION:-0}" -ge 3405 ]; then
          PROMPT_COMMAND="__vte_prompt_command"
      else
          PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;
    screen*)
      if [ -e /etc/sysconfig/bash-prompt-screen ]; then
          PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
      else
          PROMPT_COMMAND='printf "\033k%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
      fi
      ;;
    *)
      [ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
      ;;
    esac
  fi
  # Turn on parallel history
  shopt -s histappend
  history -a
  # Turn on checkwinsize
  shopt -s checkwinsize
  [ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
  # You might want to have e.g. tty in prompt (e.g. more virtual machines)
  # and console windows
  # If you want to do so, just add e.g.
  # if [ "$PS1" ]; then
  #   PS1="[\u@\h:\l \W]\\$ "
  # fi
  # to your custom modification shell script in /etc/profile.d/ directory
fi

41行目 にて、PS1 がデフォルト値(\\s-\\v\\\$ )の場合に、[\u@\h \W]\\$ が適用されるといった記述がありました。

etc/bashrc は ~/.bash_profile の後に実行される挙動になっているため、~/.bash_profile でデフォルトから書き換えてしまえばよさそうです。

補足

全ユーザーに同一の環境変数を適用させるには、/etc/profile.d/ 内に sh ファイルを作成して、環境変数を定義することが考えられます。

~/.bash_profile で PS1 の値を設定する

PS1 は表示上の可読性に寄与するため、ユーザーごとの変更でよさそうです。そのため、冒頭記載のように下記を追加することで PS1 の値を設定できます。

if [ "$PS1" ]; then
   PS1="[\u@\t \w]\$ "
fi

おわりに

小ネタのつもりで書き始めましたが、bash の設定ファイルはどういう順番で読み込むのだろうと悩むシーンがあったので、再勉強の機会になってよかったです。

どこかの誰かにお役に立てたら幸いです。

参考資料

【LPIC102】bashの設定ファイルと読み込み順序 LPICで学ぶLinux2 - SEワンタンの独学備忘録