Useful bash shell prompt with customizable colors that are switchable on the fly.

Posted: November 1, 2017. At: 9:38 AM. This was 3 months ago. Post ID: 11497
Page permalink.
WordPress uses cookies, or tiny pieces of information stored on your computer, to verify who you are. There are cookies for logged in users and for commenters. These cookies expire two weeks after they are set.

This bash script will give you a custom bash prompt that has colors that are switchable on the fly.

PS1=$(
    sep=$'\xC2\xBB'     # UTF-8 U+00BB
    red=$(tput setaf 1)
    green=$(tput setaf 2)
    yellow=$(tput setaf 3)
    blue=$(tput setaf 4)
    cyan=$(tput setaf 5)
    bold=$(tput bold)
    reset=$(tput sgr0)
 
    [[ $EUID -eq 0 ]] && user=$red || user=$green
 
    echo "\[$user\]\[email protected]\h\[$reset\] \[$blue$bold\]$sep\[$reset\] \[$yellow\]\W\[$reset\] \[$blue$bold\]$sep\[$reset\] \[$user\]\$\[$reset\] "
)

To change to a green prompt, use this command.

[email protected]:~$ tput setaf 2

And this one for a blue shell prompt.

[email protected]:~$ tput setaf 5

This is a very cool idea, this means that the color of the shell prompt may be changed whenever you wish. This is a nice way to customize the shell prompt. I guess other settings could be controlled this way too.

Run this command to print all available colors in your terminal.

for i in {17..255};{ printf '\e[38;5;%dm%d \n' "$i" "$i"; } | column

No comments have been made. Use this form to start the conversation :)

Leave a Reply