How to change bash prompt color based on exit code of last command?

前端 未结 3 1738
半阙折子戏
半阙折子戏 2021-01-05 18:58

I want to display a green smile face if the previous exit code is 0 and red smile face if not successful.

Basically I want to do this prompt but with other stuff in

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-05 19:24

    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\]\
    "
    

提交回复
热议问题