Creating a Bash command prompt with a red $ after failure of previous command

后端 未结 3 585
情书的邮戳
情书的邮戳 2021-01-13 06:03

I\'m new to Bash programming, and I\'m working on creating a custom Bash command prompt. My goal is to create a prompt which only shows the login name and h

3条回答
  •  囚心锁ツ
    2021-01-13 06:28

    Here is Alexsandr's answer modified to display the red color only when a command fails and not when you push enter on an empty command line, as Stéphane requested.

    trap 'PREVIOUS_COMMAND=$THIS_COMMAND; THIS_COMMAND=$BASH_COMMAND' DEBUG
    read -r -d '' PROMPT_COMMAND << 'END'
        if [ $? = 0 -o $? == 130 -o "$PREVIOUS_COMMAND" = ": noop" ]; then
            PS1='\[\e[32;1m\]\u@\[\e[0m\e[30;47m\]\H\[\e[0m\]:\[\e[34;1m\]\w\[\e[0m\]$ '
        else
            PS1='\[\e[31;1m\]\u@\[\e[0m\e[31;47m\]\H\[\e[0m\]:\[\e[31;1m\]\w\[\e[0m\]$ '
        fi
        : noop
    END
    

提交回复
热议问题