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.
-
1Which shell are you using? This is facile to achieve in zsh.Sparhawk– Sparhawk2018-12-09 00:05:59 +00:00Commented Dec 9, 2018 at 0:05
-
bash, whatever is standard on mac.eugenekgn– eugenekgn2018-12-09 02:28:33 +00:00Commented Dec 9, 2018 at 2:28
-
While technically OSX is based on Linux, there's an active Mac-specific SE site and this question is much more appropriate there. You'd get better answers as well.Bagalaw– Bagalaw2018-12-09 03:24:02 +00:00Commented Dec 9, 2018 at 3:24
-
2@Bagalaw I disagree. This is a purely bash question, and IMO appropriate for U/L.Sparhawk– Sparhawk2018-12-09 03:40:33 +00:00Commented Dec 9, 2018 at 3:40
-
@eugenekgn There are numerous similar questions on this site. Have a look and see if you can work it out from them.Sparhawk– Sparhawk2018-12-09 03:41:39 +00:00Commented Dec 9, 2018 at 3:41
1 Answer
Bash has some variables that let you control the prompt:
PROMPT_COMMANDPS1PS2PS3PS4
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.
-
Most people just say
"$?". I see no reason to include the{and the}; do you have one?G-Man Says 'Reinstate Monica'– G-Man Says 'Reinstate Monica'2018-12-09 05:42:20 +00:00Commented Dec 9, 2018 at 5:42 -
@G-Man It's just my personal preference. Neither the braces nor the quotes around
${?}are really needed in that case AFAIK.nxnev– nxnev2018-12-09 06:10:40 +00:00Commented Dec 9, 2018 at 6:10 -
Well, quoting shell variables is always recommended; see Security implications of forgetting to quote a variable in bash/POSIX shells, including Steven Penny's answer. My answer to another question is also relevant.G-Man Says 'Reinstate Monica'– G-Man Says 'Reinstate Monica'2018-12-09 06:27:26 +00:00Commented Dec 9, 2018 at 6:27
-
@G-Man Sorry, I didn't express myself correctly. What I meant is that there's no need to quote the
${?}in[[ "${?}" == "0" ]]because word splitting doesn't affect such variable in that specific case. I quoted it anyway because, as Stéphane Chazelas said in the question you linked, "omitting quotes [...] can send a wrong message to beginners: that it may be all right not to quote variables". For the record, I've also made an answer about some subtleties of word splitting.nxnev– nxnev2018-12-09 07:30:23 +00:00Commented Dec 9, 2018 at 7:30