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

前端 未结 3 1680
半阙折子戏
半阙折子戏 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条回答
  •  -上瘾入骨i
    2021-01-05 19:39

    I adapted Michael A's answer for a .bashrc file (in my case, 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 the default is:

    PS1='\
    \[\033]0;$TITLEPREFIX:$PWD\007\]\n\
    \[\033[32m\]\u@\h\
    \[\033[35m\] $MSYSTEM \
    \[\033[33m\]\w\
    \[\033[36m\]`__git_ps1`\
    \[\033[0m\]\n$'
    

提交回复
热议问题