Wednesday, November 21, 2012

Command prompt exit face (color smiley face)

Here's a neat little hack that you can use to display a color exit code smiley face on your shell prompt (also refer to "Playing with terminal colors on your script" for more info on color and shell).

Here's what the end result looks like:
victor@Desktop:~ :) $ cd /tmp
victor@Desktop:/tmp 
:) $ ls 123
ls: cannot access 123: No such file or directory

victor@Desktop:~ :(

To achieve that, simply change your PS1 variable in your profile or you shell rc file to the code below:
PS1='\u@\h:\w `if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\e[37m\]; fi` $ '

No comments: