CLI Color and bash prompt.. some colored fun with prompts in bash

2010-06-29 886 words 5 mins read

There are two ways to do it, one is with tput command. We will see that some other time, today we will consider the escape sequence way.

First we need to understand the colors as understood by bash. Some time back a fried of mine gave me this little script to find the colors. You can download it <a href="http://blog.amit-agarwal.co.in/2009/03/bash_colors" target="_blank">Script bash colors. Here is the script

#!/bin/bash

T=&#8217;amit agarwal&#8217;   # The test text

echo -e &#8221;\n                 40m     41m     42m     43m\
44m     45m     46m     47m&#8221;;

for FGs in &#8217;    m&#8217; &#8217;   1m&#8217; &#8217;  30m&#8217; &#8217;1;30m&#8217; &#8217;  31m&#8217; &#8217;1;31m&#8217; &#8217;  32m&#8217; \
&#8217;1;32m&#8217; &#8217;  33m&#8217; &#8217;1;33m&#8217; &#8217;  34m&#8217; &#8217;1;34m&#8217; &#8217;  35m&#8217; &#8217;1;35m&#8217; \
&#8217;  36m&#8217; &#8217;1;36m&#8217; &#8217;  37m&#8217; &#8217;1;37m&#8217;;
do FG=${FGs// /}
echo -en &#8221; $FGs \033[$FG  $T  &#8221;
for BG in 40m 41m 42m 43m 44m 45m 46m 47m;
do echo -en &#8221;$EINS \033[$FG\033[$BG  $T  \033[0m&#8221;;
done
echo;
done
echo

Now you will get output something like below:

<img class="size-medium wp-image-691" title="bash color codes" src="http://blog.amit-agarwal.co.in/wp-content/uploads/2010/08/bash_color_codes-300x95.jpg" alt="bash color codes" width="300" height="95" />

Now&#8217;s the time to get our hand dirty. First lets note down some facts

<img src="file:///tmp/moz-screenshot.jpg" alt="" />

\[ and \] respectively marks the begining and end of non-printing character. These characters also mark the start and end of non-countable characters for line wrapping.

\e is the ASCII escape character also marked by \033

PS1 holds the string for the command prompt

PS2 holds the string for the command prompt at second level.

So we will use the above two in combination with the color codes that we saw earlier to create our own colored prompt.

Let try with simple one first. Let try something like &#8221;aka>&#8221; in green color. So we will try in the terminal first:

PS1=&#8221;\[\e[1;32m\]aka>\[\e[0m\]&#8221;

Explanation:

PS1 –> we want to change the command prompt and not the second prompt

\[   –> Begin non printing character

\e[   –> Escape character

1;32m –> Color green with no background as per the chart of the output from bash_color script

\]   –> End non printing character

aka> –> Actual prompt

\[     –> Begin non printing character

\e[  –> Escape Character

0m –> Restore original color to black

\]     –> End non printing character

Ok, no now all the cryptic code above looks simple, isn&#8217;t it. Let&#8217;s see what all we can put in the command prompt:

<span style=\"color: #00ff00;\">   Sequence   Description
</span>
<span style=\"color: #00ff00;\">              \\a     an ASCII bell character (07)</span>
<span style=\"color: #00ff00;\">              \\d     the date in \"Weekday Month Date\" format (e.g., \"Tue May 26\")</span>
<span style=\"color: #00ff00;\">              \\D{format}</span>
<span style=\"color: #00ff00;\">                     the format is passed to strftime(3) and the result is inserted into the prompt string; an empty format results in  a  locale-</span>
<span style=\"color: #00ff00;\">                     specific time representation.  The braces are required</span>
<span style=\"color: #00ff00;\">              \\e     an ASCII escape character (033)</span>
<span style=\"color: #00ff00;\">              \\h     the hostname up to the first ‘.’</span>
<span style=\"color: #00ff00;\">              \\H     the hostname</span>
<span style=\"color: #00ff00;\">              \\j     the number of jobs currently managed by the shell</span>
<span style=\"color: #00ff00;\">              \\l     the basename of the shell’s terminal device name</span>
<span style=\"color: #00ff00;\">              \\n     newline</span>
<span style=\"color: #00ff00;\">              \\r     carriage return</span>
<span style=\"color: #00ff00;\">              \\s     the name of the shell, the basename of $0 (the portion following the final slash)</span>
<span style=\"color: #00ff00;\">              \\t     the current time in 24-hour HH:MM:SS format</span>
<span style=\"color: #00ff00;\">              \\T     the current time in 12-hour HH:MM:SS format</span>
<span style=\"color: #00ff00;\">              \\@     the current time in 12-hour am/pm format</span>
<span style=\"color: #00ff00;\">              \\A     the current time in 24-hour HH:MM format</span>
<span style=\"color: #00ff00;\">              \\u     the username of the current user</span>
<span style=\"color: #00ff00;\">              \\v     the version of bash (e.g., 2.00)</span>
<span style=\"color: #00ff00;\">              \\V     the release of bash, version + patch level (e.g., 2.00.0)</span>
<span style=\"color: #00ff00;\">              \\w     the current working directory, with $HOME abbreviated with a tilde</span>
<span style=\"color: #00ff00;\">              \\W     the basename of the current working directory, with $HOME abbreviated with a tilde</span>
<span style=\"color: #00ff00;\">              \\!     the history number of this command</span>
<span style=\"color: #00ff00;\">              \\#     the command number of this command</span>
<span style=\"color: #00ff00;\">              \\$     if the effective UID is 0, a #, otherwise a $</span>
<span style=\"color: #00ff00;\">              \\nnn   the character corresponding to the octal number nnn</span>
<span style=\"color: #00ff00;\">              \\     a backslash</span>
<span style=\"color: #00ff00;\">              \\[     begin a sequence of non-printing characters, which could be used to embed a terminal control sequence into the prompt</span>
<span style=\"color: #00ff00;\">              \\]     end a sequence of non-printing characters</span>

So, with the above information in hand, we are ready to create complex command prompts for our shell. If all this does not look like you can handle and need some help on this, there is always help available in the form of <a href="http://sourceforge.net/projects/bashish/" target="_blank">bashish and lot of other bash tools to help <a href="http://sourceforge.net/search/?words=bashish&type_of_search=soft&pmode=0&words=bash&Search=Search" target="_blank">here.

If you like the artile please digg it or share on your favourite bookmarks site.

Some examples from various sources:

export PS1=&#8221;\e[0;31m[\u@\h \W]\$ \e[m&#8221;

Conditional:

if [ `hostname|cut -c -11` == lonlnddebtp ]; then
PS1=’\[\e[1;31m\][\u@\h:\w]\$\[\e[0m\] ‘ # PROD, red color
elif [ `hostname|cut -c -11,13-` == lonlnddebtd-z1 ]; then
PS1=’\[\e[2;32m\][\u@\h:\w]\$\[\e[0m\] ‘ # UAT, green color
else
PS1=’[\u@\h:\w]\$ ‘
fi

set PS1 = “%B%{33[31m%}%m %{33[37m%}%B%// \n”

PS1=&#8221;\[\033[31m\]\332\304\[\033[34m\](\[\033[31m\]\u\[\033[34m\]@\[\033[31m\]\h\
\[\033[34m\])\[\033[31m\]-\[\033[34m\](\[\033[31m\]\$(date +%I:%M%P)\
\[\033[34m\]-:-\[\033[31m\]\$(date +%m)\[\033[34m\033[31m\]/\$(date +%d)\
\[\033[34m\])\[\033[31m\]\304-\[\033[34m]\371\[\033[31m\]-\371\371\
\[\033[34m\]\372\n\[\033[31m\]\300\304\[\033[34m\](\[\033[31m\]\W\[\033[34m\])\
\[\033[31m\]\304\371\[\033[34m\]\372\[\033[00m\]&#8221;
export PS1=&#8221;\[\e[32;1m\]\w> \[\e[0m\]&#8221;

export PS1=&#8221;\[\e[36;1m\]\u@\[\e[32;1m\]\H> \[\e[0m\]&#8221;
and so on….

Leave me a comment if you need some specific format for the prompt.


author

Authored By Amit Agarwal

Amit Agarwal, Linux and Photography are my hobbies.Creative Commons Attribution 4.0 International License.

We notice you're using an adblocker. If you like our webite please keep us running by whitelisting this site in your ad blocker. We’re serving quality, related ads only. Thank you!

I've whitelisted your website.

Not now
This website uses cookies to ensure you get the best experience on our website. Learn more Got it