2

I am using a Mac and I want to be able to show emoji X for every successful command that I type in and emoji Y for every command that results in failure.

Sparhawk
  • 19,941
eugenekgn
  • 131
  • 3

1 Answers1

6

Bash has some variables that let you control the prompt:

  • PROMPT_COMMAND
  • PS1
  • PS2
  • PS3
  • PS4

In this specific scenario, only PROMPT_COMMAND (code executed before printing the primary prompt) and PS1 (the primary prompt) are helpful.

And the variable ? let you know the exit status of the last command executed. For example:

command

if [[ "${?}" == '0' ]]; then
  echo 'OK'
else
  echo 'ERROR'
fi

So you just need to take advantage of these handy features:

# Using PROMPT_COMMAND
PROMPT_COMMAND='if [[ "${?}" == "0" ]]; then printf "[OK]"; else printf "[ERROR]"; fi'

# Using PS1
PS1='$(if [[ "${?}" == "0" ]]; then printf "[OK]"; else printf "[ERROR]"; fi)\$ '

Both ways would print something like this (assuming your initial prompt is $):

[OK]$ false
[ERROR]$ true
[OK]$ 

Just replace [OK] and [ERROR] with your desired emojis.

You can read the Controlling the Prompt section of Bash manual to learn more about this topic.

nxnev
  • 3,654