smiley shell prompt, or frown

I've been using the following command prompt for years

server ~/dir :) whoami
server ~/dir :) badcommand
-bash: badcommand: command not found
server ~/dir :( echo sorry
server ~/dir :) 

It's a smiley emoticon, but only when the previous command returned without error. If the exit code (stored in $?) returns any kind of error flag then the prompt is reversed.

It's silly, but surprisingly useful.

Here's how it works, in your .bashrc simply include the following

## set smiley cursor
smiley () { if [ $? == 0 ]; then echo ':)'; else echo ':('; fi; }
export PS1="[33[01;31m]h[33[01;34m] W $(smiley)[33[00m] "

The interesting point is that you can use bash functions within your PS1 environment variable. This opens up all kinds of interesting possibilities; you could display the total number of running process, cpu load, time, exit code, or even integrate with version control (like git or svn) to remind you of unsaved files.

This entry was posted in bash, shell tips. Bookmark the permalink.

Comments are closed.