Customizing PS1 prompt with multi-colored specification

For those interested, but don't want to use outside tools available from GitHub to do that, here is an example of what I did for mine.


Changes done to .bashrc for each of the two accounts, the basic User and for root.


User .bashrc:

    ###	User (regular) is rendered with green background; host with magenta background; working directory with yellow
    PS1='${debian_chroot:+($debian_chroot)}\n\[\033[102;1m\033[38:5:16m\]\u\[\033[00m\] @ \[\033[48:5:93m\033[97;1m\]\h\[\033[00m\] : \[\033[48:5:214m\033[38:5:16m\]\w\[\033[00m\]\n$ '

will give you this:

SNAPSHOT__PS1__User


Root .bashrc:

    ###	User (root) is rendered with red background; host with magenta background; working directory with yellow
    PS1='${debian_chroot:+($debian_chroot)}\n\[\033[101;1m\033[38:5:16m\]\u\[\033[00m\] @ \[\033[48:5:93m\033[97;1m\]\h\[\033[00m\] : \[\033[48:5:214m\033[38:5:16m\]\w\[\033[00m\]\n# '

will give you this:

SNAPSHOT__PS1__root


To ease testing, you can use the following as a starter set of parameter definitions (INCLUDES__TerminalEscape_SGR.bh) for testing, using the script provided at the bottom:

boldON="\033[1m"
boldOFF="\033[0m"

italicON="\033[3m"
italicOFF="\033[0m"

underlineON="\033[4m"
underlineOFF="\033[0m"

blinkON="\033[5m"
blinkOFF="\033[0m"

blackON="\033[38:5:16m"
blackOFF="\033[0m"

whiteON="\033[97;1m"
whiteOFF="\033[0m"

cyanON="\033[96;1m"
cyanOFF="\033[0m"

cyanDarkON="\033[36;1m"
cyanDarkOFF="\033[0m"

greenON="\033[92;1m"
greenOFF="\033[0m"

yellowON="\033[93;1m"
yellowOFF="\033[0m"

redON="\033[91;1m"
redOFF="\033[0m"

orangeON="\033[33;1m"
orangeOFF="\033[0m"

blueON="\033[94;1m"
blueOFF="\033[0m"

blueSteelON="\033[34;1m"
blueSteelOFF="\033[0m"

darkBlueON="\033[38:5:12m"
darkBlueOFF="\033[0m"

magentaON="\033[95;1m"
magentaOFF="\033[0m"

redBgON="\033[101;1m"
redBgOFF="\033[0m"

whiteBgON="\033[107;1m"
whiteBgOFF="\033[0m"



blackBgON="\033[40;1m"
blackBgON="\033[48:5:233m"
blackBgON="\033[48:5:0m"
blackBgOFF="\033[0m"

cyanBgON="\033[106;1m"
cyanBgOFF="\033[0m"

cyanDarkBgON="\033[46;1m"
cyanDarkBgOFF="\033[0m"

greenBgON="\033[102;1m"
greenBgOFF="\033[0m"

yellowBgON="\033[103;1m"
yellowBgON="\033[48:5:214m"
yellowBgOFF="\033[0m"

redBgON="\033[101;1m"
redBgON="\033[48:5:9m"
redBgOFF="\033[0m"

orangeBgON="\033[43;1m"
orangeBgOFF="\033[0m"

blueBgON="\033[104;1m"
blueBgOFF="\033[0m"

blueSteelBgON="\033[44;1m"
blueSteelBgOFF="\033[0m"

darkBlueBgON="\033[48:5:12m"
darkBlueBgOFF="\033[0m"

magentaBgON="\033[48:5:55m"
magentaBgON="\033[48:5:93m"
magentaBgOFF="\033[0m"

Testing script:

#!/bin/sh


. INCLUDES__TerminalEscape_SGR.bh >>/dev/null

whoHost="root@OasisMega1"
who="ericthered"
host="OasisMega1"
workDir="$(pwd)"
promptChar="$"
promptChar="#"

#TEST FOR text reading anything 12345-67890! @ # \

#${magentaBgON}${whiteON}\
#\u\
#${whiteOFF}\

#${redBgON}${blackON}\
#\u\
#${blackOFF}\

#${boldON}\
#${boldOFF}\

testor_user()
{
echo "\n\
${greenBgON}${blackON}\
\u\
${blackOFF}\
 @ \
${magentaBgON}${whiteON}\
\h\
${whiteOFF}\
 : \
${yellowBgON}${blackON}\
\w\
${blackOFF}\
\n$ "

PS1='\n\[\033[102;1m\033[38:5:16m\]\u\[\033[0m\] @ \[\033[48:5:93m\033[97;1m\]\h\[\033[0m] : \[\033[48:5:214m\033[38:5:16m\]\w\[\033[0m\]\n$ '
}
testor_user


testor_root()
{
echo "\n\
${redBgON}${blackON}\
\u\
${blackOFF}\
 @ \
${magentaBgON}${whiteON}\
\h\
${whiteOFF}\
 : \
${yellowBgON}${blackON}\
\w\
${blackOFF}\
\n$ "

PS1='\n\[\033[101;1m\033[38:5:16m\]\u\[\033[0m\] @ \[\033[48:5:93m\033[97;1m\]\h\[\033[0m] : \[\033[48:5:214m\033[38:5:16m\]\w\[\033[0m\]\n$ '
}
#testor_root
2 Likes

Very nice! I tend to like a more "Spartan" prompt, using only my user and current working directory (PS1='${PWD##*/} $ ') and set the colors in my terminal profile. But I like this (although I stripped out the hostname, since that's superfluous for me.

I do have a question though: I've seen this ${debian_chroot:+($debian_chroot)} construct and have never understood it. Care to enlighten me?

2 Likes

Thank you, Fred!

I've never understood that, exactly, but I believe that is the portion which handles the sudo change. But, do NOT take my word for that!

As for hostname, depending on which image I boot from (different internal disks: OasisMidi, OasisMega1, OasisMaxi), I have a different personality. So, I like to have the hostname flashed at me.

:slight_smile:

1 Like

I get the multiple hosts info, which is why I said it doesn't apply for my purposes. But I think it's simpler to use the bash built-ins like \u for username, \h or \H for hostname, and others. There's a nice reference to these here:

2 Likes

Yes, I use those shortforms.

The variables are only for the test script to emulate while testing to help decide the final appearance before working thru the proper escape sequencing to get everything into the .bashrc PS1 definition. :slight_smile:

1 Like

debian_chroot is used when you define a subsystem within your environment and chroot into it. For example, you can mount your current fs into /mnt/jail then chroot into it and check if a pkg works. When you chroot, PS1 prompt changes.

3 Likes

Thank you, Pavlos. I've never had occasion to need it.

I like visuals, so I dug around to find an example and found a post that fleshes that out for me:


Also, I stumbled across this handy post about how to test programatically as to which state the current shell finds itself in, in case that is needed:

1 Like

it is similar when you create a virtual env in python, the prompt become (venv).

3 Likes