0

I want to customize my terminal to display emoji faces, showing sad face for wrong command and happy face for right command that I enter. Is there any way that I can do this using my terminal bash.

Here is my PS1 from .bash_profile

export PS1="[\[\e[1;32m\]\@\[\e[m\]] [\[\e[1;34m\]\u\[\e[m\]➡ \[\e[1;31m\]\W\[\e[m\]]\$ "
  • can you define "wrong command" and "right command" -- what exactly should the terminal be trying to do for you? Is a right command any command that process and exits normally? Should this happen as you type in real time? What are you after? – samh Apr 18 at 16:58
3

Building on Maghin's post.

# Use echo -n to leave out the line end.
# Use -C option with hexdump to avoid big endian/little 
# endian confusion.
mac $  echo -n 😀 | hexdump -C
00000000  f0 9f 98 80                                       |....|
00000004
mac $  echo -n 😱  | hexdump -C
00000000  f0 9f 98 b1                                       |....|
00000004

"Include non-0 exit codes in the subsequent bash prompt" http://stackoverflow.com/questions/5946873/include-non-0-exit-codes-in-the-subsequent-bash-prompt

Here is what I ended up with:

export PS1='\u $(highlightExitCode) \$ '


highlightExitCode()
    {
        exit_code=$?
        if [ $exit_code -ne 0 ]
        then
           echo -en '\xf0\x9f\x98\xb1 '
        else
           echo -en '\xf0\x9f\x98\x80 '
        fi

    }

enter image description here

A more straight forward alternative for the highlightExitCode function:

 highlightExitCode ()  { 
   if [ $? -ne 0 ]; then
     echo -n '😱 ';
   else
     echo -n '😀 ';
   fi 
 }

The only problem is that the bad image appears until to you run a succesful command.

enter image description here

3

You can use the hexadecimal form of the icon.

Here is my method :

Copy an emoji from a graphical source : https://getemoji.com/ Then paste it in your terminal in the following command :

$ echo 😀 | hexdump
0000000 9ff0 8098 000a                         
0000005

Then take every octal and put \x before each :

$ echo -e '\xf0\x9f\x98\x80\x0a\x00'
😀
  • This is a helpful start, but is not going to display the proper emoji based on a good or bad command. – samh Apr 18 at 16:58
  • You need to know the secret handshake. You need to swap the byte displayed from hexdump. You need to ignore the odd byte & there is no need for the line-end – historystamp May 8 at 20:09
  • The correct string is: echo '\xf0\x9f\x98\x80' – historystamp May 8 at 20:11
  • Add there is no need for this hex stuff anyway. – historystamp May 8 at 20:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .