How to change the output color of echo in Linux

后端 未结 29 3580
刺人心
刺人心 2020-11-22 04:28

I am trying to print a text in the terminal using echo command.

I want to print the text in a red color. How can I do that?

相关标签:
29条回答
  • 2020-11-22 05:05

    Here there is a simple script to easily manage the text style in bash shell promt:

    https://github.com/ferromauro/bash-palette

    Import the code using:

    source bash-palette.sh
    

    Use the imported variable in echo command (use the -e option!):

    echo -e ${PALETTE_GREEN}Color Green${PALETTE_RESET}
    

    It is possible to combine more elements:

    echo -e ${PALETTE_GREEN}${PALETTE_BLINK}${PALETTE_RED_U}Green Blinking Text over Red Background${PALETTE_RESET}
    

    0 讨论(0)
  • 2020-11-22 05:06

    I'm using this for color printing

    #!/bin/bash
    #--------------------------------------------------------------------+
    #Color picker, usage: printf $BLD$CUR$RED$BBLU'Hello World!'$DEF     |
    #-------------------------+--------------------------------+---------+
    #       Text color        |       Background color         |         |
    #-----------+-------------+--------------+-----------------+         |
    # Base color|Lighter shade| Base color   | Lighter shade   |         |
    #-----------+-------------+--------------+-----------------+         |
    BLK='\e[30m'; blk='\e[90m'; BBLK='\e[40m'; bblk='\e[100m' #| Black   |
    RED='\e[31m'; red='\e[91m'; BRED='\e[41m'; bred='\e[101m' #| Red     |
    GRN='\e[32m'; grn='\e[92m'; BGRN='\e[42m'; bgrn='\e[102m' #| Green   |
    YLW='\e[33m'; ylw='\e[93m'; BYLW='\e[43m'; bylw='\e[103m' #| Yellow  |
    BLU='\e[34m'; blu='\e[94m'; BBLU='\e[44m'; bblu='\e[104m' #| Blue    |
    MGN='\e[35m'; mgn='\e[95m'; BMGN='\e[45m'; bmgn='\e[105m' #| Magenta |
    CYN='\e[36m'; cyn='\e[96m'; BCYN='\e[46m'; bcyn='\e[106m' #| Cyan    |
    WHT='\e[37m'; wht='\e[97m'; BWHT='\e[47m'; bwht='\e[107m' #| White   |
    #-------------------------{ Effects }----------------------+---------+
    DEF='\e[0m'   #Default color and effects                             |
    BLD='\e[1m'   #Bold\brighter                                         |
    DIM='\e[2m'   #Dim\darker                                            |
    CUR='\e[3m'   #Italic font                                           |
    UND='\e[4m'   #Underline                                             |
    INV='\e[7m'   #Inverted                                              |
    COF='\e[?25l' #Cursor Off                                            |
    CON='\e[?25h' #Cursor On                                             |
    #------------------------{ Functions }-------------------------------+
    # Text positioning, usage: XY 10 10 'Hello World!'                   |
    XY () { printf "\e[$2;${1}H$3"; }                                   #|
    # Print line, usage: line - 10 | line -= 20 | line 'Hello World!' 20 |
    line () { printf -v _L %$2s; printf -- "${_L// /$1}"; }             #|
    # Create sequence like {0..(X-1)}                                    |
    que () { printf -v _N %$1s; _N=(${_N// / 1}); printf "${!_N[*]}"; } #|
    #--------------------------------------------------------------------+
    

    All basic colors set as vars and also there are some usefull functions: XY, line and que. Source this script in one of yours and use all color vars and functions.

    0 讨论(0)
  • 2020-11-22 05:06

    And this what I used to see all combination and decide which reads cool:

    for (( i = 0; i < 8; i++ )); do
        for (( j = 0; j < 8; j++ )); do
            printf "$(tput setab $i)$(tput setaf $j)(b=$i, f=$j)$(tput sgr0)\n"
        done
    done
    
    0 讨论(0)
  • 2020-11-22 05:07

    You may "combined" colours and text-mode.

    #!/bin/bash
    
    echo red text / black background \(Reverse\)
    echo "\033[31;7mHello world\e[0m";
    echo -e "\033[31;7mHello world\e[0m";
    echo
    
    echo yellow text / red background
    echo "\033[32;41mHello world\e[0m";
    echo -e "\033[32;41mHello world\e[0m";
    echo "\033[0;32;41mHello world\e[0m";
    echo -e "\033[0;32;41mHello world\e[0m";
    echo
    
    echo yellow BOLD text / red background
    echo "\033[1;32;41mHello world\e[0m";
    echo -e "\033[1;32;41mHello world\e[0m";
    echo
    
    echo yellow BOLD text underline / red background
    echo "\033[1;4;32;41mHello world\e[0m";
    echo -e "\033[1;4;32;41mHello world\e[0m";
    echo "\033[1;32;4;41mHello world\e[0m";
    echo -e "\033[1;32;4;41mHello world\e[0m";
    echo "\033[4;32;41;1mHello world\e[0m";
    echo -e "\033[4;32;41;1mHello world\e[0m";
    echo
    

    0 讨论(0)
  • 2020-11-22 05:08

    This question has been answered over and over again :-) but why not.

    First using tput is more portable in modern environments than manually injecting ASCII codes through echo -E

    Here's a quick bash function:

     say() {
         echo "$@" | sed \
                 -e "s/\(\(@\(red\|green\|yellow\|blue\|magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\4@reset/g" \
                 -e "s/@red/$(tput setaf 1)/g" \
                 -e "s/@green/$(tput setaf 2)/g" \
                 -e "s/@yellow/$(tput setaf 3)/g" \
                 -e "s/@blue/$(tput setaf 4)/g" \
                 -e "s/@magenta/$(tput setaf 5)/g" \
                 -e "s/@cyan/$(tput setaf 6)/g" \
                 -e "s/@white/$(tput setaf 7)/g" \
                 -e "s/@reset/$(tput sgr0)/g" \
                 -e "s/@b/$(tput bold)/g" \
                 -e "s/@u/$(tput sgr 0 1)/g"
      }
    

    Now you can use:

     say @b@green[[Success]] 
    

    to get:

    Notes on portability of tput

    First time tput(1) source code was uploaded in September 1986

    tput(1) has been available in X/Open curses semantics in 1990s (1997 standard has the semantics mentioned below).

    So, it's (quite) ubiquitous.

    0 讨论(0)
提交回复
热议问题