Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm trying to figure out how I can customize my terminal's bash prompt to use smiley faces. What I want (as seen in the example blow) is for the cwd to be separated from the prompt by a \n and show a green smiley face if the command succeeded, and a red sad face if it failed.

Any ideas?

This was inspired by a Peepcode screencast.

Example

share|improve this question
add comment

2 Answers

up vote 10 down vote accepted

After spending about a half hour playing around with andhrimnir's code and doing further research, I finally got what I wanted.

PS1="\w \`if [ \$? = 0 ]; then echo -e '\[\e[01;32m\]\n\xE2\x98\xBA'; else echo -e '\[\e[01;31m\]\n\xE2\x98\xB9'; fi\` \[\e[01;34m\]\[\e[00m\]"

You can find a list of emoticons here and then convert them to the 3-digit byte code you see after the newline character.

To get the cwd, all I had to do was use \w. You could also show the current user by doing \u@\w, which would output something like joshsmith@~.

share|improve this answer
add comment

It appears that the smiley face shown above is unicode character 0x263a. So you'll need a unicode-capable terminal (Not sure if terminal.app supports this, I imagine it does though).

Here's sample code that prints a green smiley face for return codes of 0 and red frown faces otherwise.

PS1="\[\e[01;32m\]\u@\h \[\e[01;34m\]\W \`if [ \$? = 0 ]; then echo -e '\[\e[01;32m\]:)'; else echo -e '\[\e[01;31m\]:('; fi\` \[\e[01;34m\]$\[\e[00m\]"

Credit goes to Fingel on the Arch forums (he posted it here).

share|improve this answer
    
Awesome work on the if/then sample. I'm curious how to do the newline, remove the $, actually use the Unicode symbol, and make it show the full cwd. –  Josh Smith Dec 16 '11 at 2:27
    
...and an hour later, answered my own question (thanks to you!). –  Josh Smith Dec 16 '11 at 3:26
1  
Glad I could help! There's a lot of info in the thread at the link I posted above if you want to get deeper into custom $PS1 stuff. –  andhrimnir Dec 16 '11 at 4:24
    
Yeah, half of my research started at that thread. Super helpful. Also, for anyone that wants to go more in-depth on the command line, Peepcode has a great advanced screencast. –  Josh Smith Dec 21 '11 at 18:14
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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