First, we create a set of variables that can set foreground colors, background colors, and perform various text formatting operations. Note: the script doesn't use all of these variables, but they're included for completeness.
# Set tput color aliases
FGBLACK=$(tput setaf 0)
FGRED=$(tput setaf 1)
FGGREEN=$(tput setaf 2)
FGYELLOW=$(tput setaf 3)
FGBLUE=$(tput setaf 4)
FGMAGENTA=$(tput setaf 5)
FGCYAN=$(tput setaf 6)
FGWHITE=$(tput setaf 7)
BGBLACK=$(tput setab 0)
BGRED=$(tput setab 1)
BGGREEN=$(tput setab 2)
BGYELLOW=$(tput setab 3)
BGBLUE=$(tput setab 4)
BGMAGENTA=$(tput setab 5)
BGCYAN=$(tput setab 6)
BGWHITE=$(tput setab 7)
TPBLINK=$(tput blink)
TPDIM=$(tput dim)
TPRMSO=$(tput rmso)
TPSO=$(tput smso)
TPBOLD=$(tput bold)
TPCLEAR=$(tput sgr0)
Next, we create a bash function called 'setprompt' that will check the current user's EUID and set a $FGUSER variable to set a custom prompt color for root and non-root users (in this example root user color is red, non-root user color is green.) Then it will export the custom PS1 prompt using various color/formatting variables and bash's built-in special characters for date, time, username, hostname, current path, & prompt. (Hint: run 'man bash' or visit this article to view the available special character options.)
setprompt()
{
# Set root/user options
if (( EUID == 0 )); then
# Set foreground=red
FGUSER=$(tput setaf 1)
else
# Set foreground=red
FGUSER=$(tput setaf 2)
fi
export PS1="\[$TPBOLD$FGCYAN$BGBLACK\]\D{%Y/%m/%d}\[$TPCLEAR\] \[$TPBOLD$FGYELLOW$BGBLACK\]\@\[$TPCLEAR\] \[$TPBOLD$FGUSER$TPSO\]\u@\h:\[$TPRMSO\] \w\[$TPCLEAR\] \\$ "
}
Lastly, we invoke the 'setprompt' function.
setprompt
Create a shell script in the /etc/profile.d/ directory, set execute permissions, and enjoy your beautiful new prompt!
Nerd minutia: the functions above utilize the 'tput' format/color settings, which can illicit rage if your terminal does not properly support tput or renders the format/color incorrectly. Feel free to use the hard-coded ASCII values in your variables instead - see here for a jumping-off point.