BASH: different colour for text entered by user

2019-09-02 03:55发布

I've been tidying up my BASH prompt.

Currently it looks like this:

enter image description here

Here is the code:

# NOTE: OSX uses .bashprofile  http://superuser.com/questions/244964/mac-os-x-bashrc-not-working

# http://mywiki.wooledge.org/BashFAQ/037
bold=$( tput bold || tput md )
black=$( tput setaf 0 )
red=$( tput setaf 1 )
green=$( tput setaf 2 )
blue=$( tput setaf 4 )
white=$( tput setaf 7 || tput AF 7 )
RESET=$( tput sgr0 )

# https://github.com/sickill/stderred
export DYLD_INSERT_LIBRARIES="/usr/lib/libstderred.dylib${DYLD_INSERT_LIBRARIES:+:$DYLD_INSERT_LIBRARIES}"
export STDERRED_ESC_CODE="$bold$red"

pre_prompt () 
{  
    if [ $? = 0 ]; then 
        echo "$green ✔";
    else 
        echo "$red ✘"; 
    fi

    printf "$RESET\n"
    printf "$bold"
    printf "%s@%s ~ %s:\n" "$USER" "$HOSTNAME" "$PWD"
    printf "$RESET"
}

# execs before prompt
export PROMPT_COMMAND=pre_prompt

# \[ ... \] --> http://mywiki.wooledge.org/BashFAQ/053
export PS1="\[$bold$blue\] ⤐  \[$RESET$bold\]"

export PS2="-2-> "
export PS3="-3-> "
export PS4="-4-> "

Note I'm using a super little piece of code by sickill to have STDERR get printed in red.

The only thing that I would still like to improve is to hilight text entered by the user.

Is there any way to do this?

1条回答
贪生不怕死
2楼-- · 2019-09-02 04:30

After your $RESET, add $bold plus whatever color code you want to use to highlight the commandline entry.

查看更多
登录 后发表回答