I wanted to modify my PS1 to run some commands every time. Let's say I want it so that if the last executed command was successful, it would add a green smile at the end of PS1, otherwise the smile should be red.
I extracted it to a function:
function exit_smile {
EXITSTATUS="$?"
RED="\[\e[1;31m\]"
GREEN="\[\e[32;1m\]"
if [ "${EXITSTATUS}" -eq 0 ]
then
SMILE="${GREEN}:)"
else
SMILE="${RED}:("
fi
echo -n "$SMILE"
}
and then tried both using `exit_smile`
and \$(exit_smile)
when modifying the PS1 variable, but it executes it once when modifying PS1 or prints literal \[\e...\]
instead of a color.
For example
PROMPT="\u@\h \W"
PS1="${PROMPT} \$ \$(exit_smile) ${OFF}\n"
Gives username@hostname ~ $ \[\e[32;1m\]:)
What am I missing?
printf "%s\n" "$PS1"
; it should show all the dollar signs that you want to have evaluated when the prompt is issued. – G-Man Says 'Reinstate Monica' Dec 04 '17 at 02:08\[..\]
. They're already in place here, so this isn't much of a duplicate – ilkkachu Dec 04 '17 at 08:36