How can you customize your terminal bash prompt with smiley faces?
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).
Related videos on Youtube
Josh Smith
Updated on September 18, 2022Comments
-
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.
-
Josh Smith over 12 yearsAwesome 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 fullcwd
. -
Josh Smith over 12 years...and an hour later, answered my own question (thanks to you!).
-
jake-low over 12 yearsGlad 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 over 12 yearsYeah, 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.