This site is now 100% read-only, and retired.

XML logo

Computed colors in prompt
Posted by mcortese on Tue 17 Apr 2012 at 15:02
Tags: none.
It is amazing to count the number of substitutions and expansions that the PS1 variable undergoes before Bash splits it out as a prompt. Besides the documented 1-letter codes like \h or \w, and the general variable expansion like $EUID or even $?, I've recently learned that arithmetic expansion is possible as well.

The following code produces a green prompt in normal cases, which turns to red when the last command returned a non-zero code:

PS1='\[\e[$(($? ? 31 : 32))m\]whatever you want\[\e[39m\]'

How does it work? You should recognize the arithmetic expansion $(( ... )). Inside that, $? stands for the return code of the last command, and the construct expr ? expr-then : expr-else works like in C. So the whole arithmetic clause is evaluated as either 31 (if the return code is anything but zero) or 32 (if the return code is zero).

Now the sequence ESC[31m happens to be interpreted by the terminal as 'switch to red ink', while ESC[32m means 'switch to green ink'. That's it.

A complete example is then:

PS1='\[\e[$(($??31:32))m\]$? \w\[\e[39m\]: '


Comments on this Entry

Re: Computed colors in prompt
Posted by Anonymous (71.173.xx.xx) on Wed 18 Apr 2012 at 00:46
Thanks, for something fun and usefull.

[ Parent ]