PS1真牛逼

话不多说,上代码

_set_prompt () {

        #see:  http://misc.flogisoft.com/bash/tip_colors_and_formatting
        #Reset          \e[0m

        ## Formatting:
        #Bold           \e[1m
        #Dim            \e[2m
        #Underlined     \e[4m

        ## 8/16 Colors:         9X                      4X                      10X
        #Default fg     \e[39m                          Default bg
        #Black          \e[30m  Dark gray       bg Black        bg Dark gray
        #Red            \e[31m  Light           bg                      bg Light
        #Green          \e[32m  Light           bg                      bg Light
        #Yellow         \e[33m  Light           bg                      bg Light
        #Blue           \e[34m  Light           bg                      bg Light
        #Magenta        \e[35m  Light           bg                      bg Light
        #Cyan           \e[36m  Light           bg                      bg Light
        #Light gray     \e[37m  White           bg                      bg White

        _last_exit_code=$? # Must come first!
        C_EC='\[\e[1;37m\]'$(printf '%3s' ${_last_exit_code})'\[\e[0m\]'

        #todo: set encoding to UTF-8 !
        FancyX='\342\234\227'      # ✗ ✘
        Checkmark='\342\234\223'   # ✓ ✔
        C_Fail='\[\e[1;31m\]'${FancyX}'\[\e[0m\]'
        C_Ok='\[\e[32m\]'${Checkmark}'\[\e[0m\]'
        C_Time='\[\e[2;37m\]''\t''\[\e[0m\]'

        C_NormalUser='\[\e[2;33m\]''\u''\[\e[0m\]'
        C_RootUser='\[\e[1;35m\]''\u''\[\e[0m\]'

        if [ $(uname -s) == "Darwin" ]; then
                _ip_addr=$(ipconfig getifaddr $(netstat -nr | awk '{ if ($1 ~/default/) { print $6} }'))
        elif [ $(uname -s) == "Linux" ]; then
                # may print $(NF-2)
                #_ip_addr=$(ip route | awk '/ src / {print $NF}' | head -1 )
                _ip_addr=$(ip route | grep -oP '(?<=src )[0-9.]+' | tail -1 )
        fi

        C_Host='\[\e[1;33m\]'$(hostname -A | awk '{print $1}')'\[\e[0m\]','\[\e[4;32m\]'${_ip_addr:-\h}'\[\e[0m\]'
        C_Pwd='\[\e[36m\]''\w''\[\e[0m\]'
        C_Marker='\[\e[1;37m\]''\$''\[\e[0m\]'

        git diff --exit-code --quiet HEAD >/dev/null 2>&1
        _git_diff_exit_code=$?
        if [ ${_git_diff_exit_code} -eq 1 ]; then
                C_Marker='\[\e[101m\]'*'\[\e[0m\]'" ${C_Marker}"
        elif [ ${_git_diff_exit_code} -eq 0 ]; then
                C_Marker='\[\e[102m\]'='\[\e[0m\]'" ${C_Marker}"
        fi


        # Add a bright white exit status for the last command
        PS1="$C_EC "

        # If it was successful, print a green check mark.
        # Otherwise, print a red X.
        if [[ ${_last_exit_code} -eq 0 ]]; then
                PS1+="$C_Ok "
        else
                PS1+="$C_Fail "
        fi

        # print HH:ii:ss
        PS1+="$C_Time "

        # If root, just print the host in red. Otherwise,
        # print the current user and host in green.
        if [[ $EUID -eq 0 ]]; then
                PS1+="${C_RootUser}@${C_Host}:"
        else
                PS1+="${C_NormalUser}@${C_Host}:"
        fi

        # Print the working directory and prompt marker
        PS1+="$C_Pwd $C_Marker "
}
PROMPT_COMMAND='_set_prompt'

效果如下

如果上一条命令执行成功,返回值为0,并且会显示一个对勾

如果上一条命令执行失败,返回值在第一位,并且会显示一个x