LoginSignup
15
14

More than 5 years have passed since last update.

シェルプロンプトの色分け設定

Last updated at Posted at 2014-07-10

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みたいなカラーコードについてはまた別途。

15
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
15
14