Bash Prompt with Last Exit Code

后端 未结 7 1739
余生分开走
余生分开走 2020-11-30 22:12

So, I\'ve been trying to customize by bash prompt so that it will look like

[feralin@localhost ~]$ _

with colors. I managed to get constant

7条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-30 23:06

    I wanted to keep default Debian colors, print the exact code, and only print it on failure:

    # Show exit status on failure.
    PROMPT_COMMAND=__prompt_command
    
    __prompt_command() {
        local curr_exit="$?"
    
        local BRed='\[\e[0;91m\]'
        local RCol='\[\e[0m\]'
    
        PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    
        if [ "$curr_exit" != 0 ]; then
            PS1="[${BRed}$curr_exit${RCol}]$PS1"
        fi
    }
    

提交回复
热议问题