Dec 18, 2004 15:32
I like colorful life very much. And unfortunately my terminal window is pretty significant part of my life :-( So, I like the nice and colorful prompt - The hostname of a current machine in green, and the prompt itself in red or blue background (depends on exit status of last command).
There are two code fragments, one is for color definitions only, and the second one is prompt setting. Just insert these fragments to ~/.bash_profile and ~/.bashrc files and you prompt will be nice. And you never miss the bad status of the last shell command.
First of all add color definitions (~/.bash_profile is probably the best place for that):
# Colors definitions
export fg_black=$'\e[30m'
export fg_red=$'\e[31m'
export fg_green=$'\e[32m'
export fg_yellow=$'\e[33m'
export fg_blue=$'\e[34m'
export fg_magenta=$'\e[35m'
export fg_cyan=$'\e[36m'
export fg_white=$'\e[37m'
export fg_black_bold=$'\e[30;1m'
export fg_red_bold=$'\e[31;1m'
export fg_green_bold=$'\e[32;1m'
export fg_yellow_bold=$'\e[33;1m'
export fg_blue_bold=$'\e[34;1m'
export fg_magenta_bold=$'\e[35;1m'
export fg_cyan_bold=$'\e[36;1m'
export fg_white_bold=$'\e[37;1m'
export bg_black=$'\e[40m'
export bg_red=$'\e[41m'
export bg_green=$'\e[42m'
export bg_yellow=$'\e[43m'
export bg_blue=$'\e[44m'
export bg_magenta=$'\e[45m'
export bg_cyan=$'\e[46m'
export bg_white=$'\e[47m'
export normal=$'\e[0m'
Now prompt-related code itself (~/.bashrc is a pretty good place for it)
set_promtp_colors()
{
if (( $? ))
then
p_beg=${bg_red}${fg_yellow_bold}
p_end=${normal}
else
p_beg=${bg_blue}${fg_yellow_bold}
p_end=${normal}
fi
h_beg=${fg_green_bold}
h_end=${normal}
r_beg=${bg_red}${fg_white_bold}
r_end=${normal}
}
PROMPT_COMMAND=set_promtp_colors
if [[ "`whoami`" == "root" ]]
then
PS1=' \[${r_beg}\]ROOT\[${r_end}\] \[${h_beg}\]${HOSTNAME}\[${h_end}\] \
\[${p_beg}\]<\!>\[${p_end}\] '
else
PS1=' \[${h_beg}\]${HOSTNAME}\[${h_end}\] \[${p_beg}\]<\!>\[${p_end}\] '
fi