I am using Ubuntu and can manually change the bash shell prompt color to green using

export PS1="\e[0;32m[\u@\h \W]\$ \e[m"

However, I want the shell prompt color to automatically change whenever I open a new terminal or tab. I am aware that the basic tty TERM has 16 colors, and it's okay to rotate the colors if more than 16 terminals are open. Will the solution also work when I connect through Putty, tmux or screen.

My idea is to write a shell script and place it in .bashrc which detects the new terminal session the user has opened and increment a global counter from \e[0;31m[ to \e[0;47m[. How to detect the number of opened terminals by the user?


Counting the terminals (ex: who - a | grep user | wc -l ) will not work : when one or seceral shell are closed, the total number decreases and the new terminal may match another one already opened.

You should simply have a counter:

If you want 6 colors overall:

touch ~/.colornumber
new=$(awk '(NR==1) { print ($1 + 1) % 6; }' ~/.colornumber)
echo $new > ~/.colornumber
#and use color number $new for your current terminal, for example defined in an array of 6 entries. 
 # just note here that color 0 is for the 6th terminal, not the first... or change the awk above

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.