CentOSのプロンプトの色を変更するときにやること。
本番環境と開発環境とで色を変えたりすると分かりやすいのでお勧めです。
6.5でやってますが旧バージョンや他のディストリビューションでも問題無いと思います。
環境
- CentOS 6.5
特定ユーザの色分け設定
特定のユーザのプロンプトを変更する場合には~/.bashrc
を下記のように変更する。
~/.bashrc
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# User specific aliases and functions
export PS1="\[\e[1;31m\][\u@\h \W]\\$\[\e[m\] " ⇐追記
それぞれの項目の意味は下記の通り
項目 | 意味 |
---|---|
\[\e[1;31m\] | 色分けしたい文字の先頭に付ける記号 |
\[\e[m\] | 色分けしたい文字の末尾に付ける記号 |
\u | ユーザ名 |
\h | ホスト名 |
\W | カレントディレクトリ |
\$ | 一般ユーザーならドル記号、rootユーザーならシャープ記号になる |
注意が必要なのは\$
で、\\$
の様にバックスラッシュでエスケープしてあげないと常に$
が表示されることになる。
全ユーザの色分け設定
全のユーザのプロンプトを変更する場合には/etc/bashrc
を下記のように変更する。
/etc/bashrc 31行目 変更前
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
/etc/bashrc 31行目 変更後
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="\[\e[1;31m\][\u@\h \W]\\$\[\e[m\] "
カラーコードについて
1;31
みたいなカラーコードについてはまた別途。