Posted: . At: 9:38 AM. This was 6 years 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.


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


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\]\u@\h\[$reset\] \[$blue$bold\]$sep\[$reset\] \[$yellow\]\W\[$reset\] \[$blue$bold\]$sep\[$reset\] \[$user\]\$\[$reset\] "
)

To change to a green prompt, use this command.

jason@jason-desktop:~$ tput setaf 2

And this one for a blue shell prompt.

jason@jason-desktop:~$ 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

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.