From SpectLog
Jump to: navigation, search

The shell prompt is controlled by PS1 special variable. Various different information which can be encoded into the prompt is described in PROMPTING section of man 1 bash page.


The prompt colors are modified by the terminal control sequence embedded into PS1 string between \[ and \] sequence. There are various references on the Internet describing the codes [1][2].

Examples

The following command makes prompt display username, hostname, date, time, number of currently running background jobs (if any) and full path to current directory - all highlighted by different colors:

PS1="\[\e[32m\]\u@\h \[\e[36m\]\D{%Y-%m-%d %H:%M:%S} \[\e[31m\]\$(if [ \$(jobs -r | wc -l) != 0 ] ; then echo 'jobs:'\$(jobs -r | wc -l)' ' ; fi)\[\e[33m\]\$(pwd)\[\e[0m\]\n$"

It is normally configured in ~/.bash_profile persistently per user.


For system-wide default configuration there is another useful requirement to highlight whether the shell is run by super user or not. For example, create /etc/profile.d/local.sh with the following script to set system-wide enhanced shell prompt where username and host are set to green for regular user and red for super user:

if [ $UID == "0" ]
then
    echo "SUPER USER" >&2
    PS1="\[\e[31m\]\u@\h \[\e[36m\]\$(if [ \$(jobs -r | wc -l) != 0 ] ; then echo 'jobs:'\$(jobs -r | wc -l)' ' ; fi)\[\e[33m\]\$(pwd)\[\e[0m\]\n$"
else
    echo "REGULAR USER" >&2
    PS1="\[\e[32m\]\u@\h \[\e[36m\]\$(if [ \$(jobs -r | wc -l) != 0 ] ; then echo 'jobs:'\$(jobs -r | wc -l)' ' ; fi)\[\e[33m\]\$(pwd)\[\e[0m\]\n$"
fi

This shell prompt does not include date and time to be shorter.