How can you customize your terminal bash prompt with smiley faces?

7,603

Solution 1

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@~.

Solution 2

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:
7,603

Related videos on Youtube

Josh Smith
Author by

Josh Smith

Updated on September 18, 2022

Comments

  • Josh Smith
    Josh Smith over 1 year

    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

  • Josh Smith
    Josh Smith over 12 years
    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
    Josh Smith over 12 years
    ...and an hour later, answered my own question (thanks to you!).
  • jake-low
    jake-low over 12 years
    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.
  • Josh Smith
    Josh Smith over 12 years
    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.