Changing the colour of the BASH shell prompt
Article about: bash shell, bashrc, change, command, prompt, shell, shell prompt
It's possible to change the contents and colour of the BASH shell prompt. My SSH sessions use a white background and I find the default [chris@localhost ~] style a little boring. I also find it useful to have the prompt green for ordinary user sessions, and red for root sessions so it's easy to see how much damage you can do by colour alone.
Changing your own bash shell prompt
To change the colour of the BASH shell prompt for yourself only, you can edit your ~/.bashrc file by adding the following to the end:
if [ $TERM = 'xterm' ]
then
PS1='\[\033[02;32m\]\u@\h\[\033[02;34m\] \w \$\[\033[00m\] '
else
PS1='\u@\h \w \$ '
fi
Then run:
. ~/.bashrc
to load the settings. Now your shell prompt will be green like so:
chris@localhost ~ $
It's possible to change it to other colours and add additional information to the shell prompt. Run man bash and locate the section titled "Prompting" for more information.
Changing the root bash shell prompt
This is the same principle as the above, but log in as root first and add the following to the end of root's ~/.bashrc:
if [ $TERM = 'xterm' ]
then
PS1='\[\033[01;31m\]\u@\[\033[01;31m\]\h\[\033[01;34m\] \W \$\[\033[00m\] '
else
PS1='\u@\h \w \$ '
fi
Now do . ~/.bashrc and the prompt will be red like so:
root@localhost ~ #
Changing the default setting for new users
You can change the bash shell prompt for new users that are created by adding the green prompt (or whatever you choose to do with it) to /etc/skel/.bashrc. When you create a new user with the command useradd -m [username] then the files in /etc/skel will be copied to the newly created user home directory.
Making the change global
If you want to make this change apply to all existing users (unless otherwise overridden by their local ~/.bashrc file) you need to edit the /etc/bashrc file. The particular section you need to edit looks like this:
if [ "$PS1" ]; then
case $TERM in
xterm*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
else
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
fi
;;
screen)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='echo -ne "\033_${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\033\\"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
fi
After some playing around with it, I couldn't work out how to modify the PROMPT_COMMAND value correctly, but adding in the PS1 setting directly underneath it did the trick:
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
PS1='\[\033[02;32m\]\u@\h\[\033[02;34m\] \w \$\[\033[00m\] '











