How do I change the bash prompt color based on the exit code of the last command?
I want to display a green smile face if the previous exit code is 0 and a red smile if it failed.
Basically I want to do this, but with other stuff included.
PS1='\u@\h:\w `if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\e[37m\]; fi` $ '
I want to abstract the function state logic, but when I try to combine these two escape characters instead of colors.
smiley()
{
if [ $? == 0 ]; then
echo ':)'
else
echo ':('
fi
}
RED="\033[1;5;91m"
GREEN="\033[1;5;92m"
NONE="\033[m"
NORMAL="\[\033[0m\]"
YELLOW="\[\033[1;4;93m\]"
MAGENTA="\[\033[35m\]"
WHITE="\[\033[1;37m\]"
BLINK="\[\033[5m\]"
#INVERT="\[\e[7m\]"
#OFF="\[\033[m\]"
PS1="${YELLOW}\u${MAGENTA}@${YELLOW}\h${NORMAL}:${WHITE}\w $(smiley)\n"
I even tried one line, but it didn't work either.
PS1='\[\033[1;4;93m\]\u\[\033[35m\]@\[\033[1;4;93m\]\h\[\033[0m\]\[\033[1;37m\] \W if [ $? = 0 ]; then echo \[\e[32m\]:\)\[\e[37m\]; else echo \[\e[31m\]:\(\[\ e[37m\]; fi\n'
If there is a way to do this without PROMPT_COMMAND?
source to share
I am assuming your quoting is incorrect. I played around with this a bit and finally got it working:
$ bash --version
GNU bash, version 4.4.12(3)-release (i686-pc-cygwin)
$ smiley()
> {
> if [ "$?" == "0" ]; then
> echo -e '\e[0;32m:) '
> else
> echo -e '\e[0;31m:( '
> fi
> }
$ PS1="$PS1"'`smiley`'
$ :) rm non-existing
rm: cannot remove 'non-existing': No such file or directory
$ :( echo "Everything fine"
Everything fine
$ :)
I did it on Windows (64 bit), but I think it should work on Linux (or any other Unix-like).
Notes:
-
I wrote a function
smiley()
(simplified version) and tested it by calling it from the command line. It worked fine. -
I added it to
PS1
, and it repeated anyway:)
. I realized that the bash replacement was already done on assignmentPS1
. -
So I protected the call with
smiley
an extra pair of single quotes to defer the call until the prompt exits. It now works as expected. -
Since the questionnaire needed a color version, I made an update. I found the actual solution at this link: SO: How to change the output echo color in Linux . Easily find the trailing escape sequences you need. The trick is to use
echo -e
it to activate the backslash inecho
.
The snapshot below shows what it looks like (with colors):
source to share
This works for me:
export PS1="\
\$(if [[ \$? -gt 0 ]]; then printf \"\\[\\033[01;31m\\]\xF0\x9F\x98\xA7\"; else printf \"\\[\\033[01;32m\\]\xF0\x9F\x98\x83\"; fi)\
\[\033[01;30m\]\u\
\[\033[01;33m\]@\
\[\033[01;36m\]\h \
\[\033[01;33m\]\w \
\[\033[01;31m\][\$(__git_ps1 '%s')] \
\[\033[01;35m\]\\$ \
\[\033[00m\]\
"
source to share
I adapted Michael's answer for a file .bashrc
(in my case it's git-for-windows)
PS1='\
'if [[ $? -gt 0 ]]; then printf "\[\033[01;31m\]:("; else printf "\[\033[01;32m\]:)"; fi'\
\[\033]0;$TITLEPREFIX:$PWD\007\] \
\[\033[32m\]\u@\h \
\[\033[33m\]\w\
\[\033[36m\]'__git_ps1'\
\[\033[0m\]\n$'
For reference by default:
PS1='\
\[\033]0;$TITLEPREFIX:$PWD\007\]\n\
\[\033[32m\]\u@\h\
\[\033[35m\] $MSYSTEM \
\[\033[33m\]\w\
\[\033[36m\]'__git_ps1'\
\[\033[0m\]\n$'
source to share