BASH: different colour for text entered by user

五迷三道 提交于 2019-12-25 12:38:06

问题


I've been tidying up my BASH prompt.

Currently it looks like this:

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:


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



来源:https://stackoverflow.com/questions/21367236/bash-different-colour-for-text-entered-by-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!