Ubuntuのコマンドプロンプトの色彩設定が反映されない問題
/etc/profileの設定の一部が反映されないぞ?
試した環境
Ubuntu(Ubuntu Server 22.04 LTS (HVM), SSD Volume Type)
Amazon Linux2023(Amazon Linux 2023 AMI)
設定した内容
profile(/etc/profile)に以下を設定。
case ${UID} in
0)
PS1='\[\e[1;35m\]\H\[\e[0m\] \[\e[4;31m\]\[\e[1;31m\]${PWD}\[\e[0m\]\[\e[1;31m\]\$\[\e[0m\] '
PS2='\[\e[1;31m\]>\[\e[0m\] '
;;
*)
PS1='\[\e[0;35m\]\H\[\e[0m\] \[\e[4;34m\]${PWD}\[\e[0m\]\[\e[0;34m\]\$\[\e[0m\] '
PS2='\[\e[0;34m\]>\[\e[0m\] '
;;
esac
この設定を行うと、ルートユーザ・スーパーユーザの時にコマンドプロンプトのユーザ名のところがマゼンタに、それ以外のユーザの時は青色になる。
何が起こったか?
profileを更新した後、sourceコマンドで読み込めばうまく反映されるが一度ログアウトし次にログインした場合は色彩設定が反映されない。
(他のbash設定は反映される)
同じ要領でAmazon Linux2023の設定を行った場合は問題なく反映される。
原因
~/.bashrcのこの設定が被っていた。
41 if [ -n "$force_color_prompt" ]; then
42 if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
43 # We have color support; assume it's compliant with Ecma-48
44 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
45 # a case would tend to support setf rather than setaf.)
46 color_prompt=yes
47 else
48 color_prompt=
49 fi
50 fi
51
52 if [ "$color_prompt" = yes ]; then
53 PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
54 else
55 PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
56 fi
57 unset color_prompt force_color_prompt
58
59 # If this is an xterm set the title to user@host:dir
60 case "$TERM" in
61 xterm*|rxvt*)
62 PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
63 ;;
64 *)
65 ;;
66 esac
Ubuntuの設定ファイル群はログイン時に以下の順で読み込まれるとのこと(ちょっと違う部分があるかも)
今回は、ログイン時に/etc/prifileの読み込み後に、それを上書きする~/.bashrc設定が適用されてしまっていた。
/etc/environment: システム全体の環境変数設定
/etc/profile: システム全体のプロファイル設定
/etc/profile.d/: /etc/profileから読み込まれるスクリプト群
~/.bash_profileまたは~/.bash_loginまたは~/.profile: ユーザーごとのプロファイル設定(存在するファイルの中で最初に見つかったものが読み込まれる)
~/.bashrc: ユーザーごとのBashシェルの設定
~/.bash_logout: ユーザーがログアウトする際に実行
~/.bash_aliases: 通常、~/.bashrcから読み込まれる
~/.bash_history: ユーザーのBashコマンドの履歴を保存
解決策
被る設定をコメントアウト(ゴリ押し)した。
36 # uncomment for a colored prompt, if the terminal has the capability; turned
37 # off by default to not distract the user: the focus in a terminal window
38 # should be on the output of commands, not on the prompt
39 #force_color_prompt=yes
40
41 #if [ -n "$force_color_prompt" ]; then
42 # if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
43 # We have color support; assume it's compliant with Ecma-48
44 # (ISO/IEC-6429). (Lack of such support is extremely rare, and such
45 # a case would tend to support setf rather than setaf.)
46 # color_prompt=yes
47 # else
48 # color_prompt=
49 # fi
50 #fi
51
52 #if [ "$color_prompt" = yes ]; then
53 # PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
54 #else
55 # PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
56 #fi
57 #unset color_prompt force_color_prompt
58
59 # If this is an xterm set the title to user@host:dir
60 #case "$TERM" in
61 #xterm*|rxvt*)
62 # PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
63 # ;;
64 #*)
65 # ;;
66 #esac
color_prompt=noまたは空を設定すれば反映されるかと思ったが反映されなかったため、ゴリ押しでコメントアウトした。
もっとスマートな方法が分かればこの記事を書き直そうと思う。