How to change the output color of echo in Linux

后端 未结 29 3583
刺人心
刺人心 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 04:53

    These codes work on my Ubuntu box:

    enter image description here

    echo -e "\x1B[31m foobar \x1B[0m"
    echo -e "\x1B[32m foobar \x1B[0m"
    echo -e "\x1B[96m foobar \x1B[0m"
    echo -e "\x1B[01;96m foobar \x1B[0m"
    echo -e "\x1B[01;95m foobar \x1B[0m"
    echo -e "\x1B[01;94m foobar \x1B[0m"
    echo -e "\x1B[01;93m foobar \x1B[0m"
    echo -e "\x1B[01;91m foobar \x1B[0m"
    echo -e "\x1B[01;90m foobar \x1B[0m"
    echo -e "\x1B[01;89m foobar \x1B[0m"
    echo -e "\x1B[01;36m foobar \x1B[0m"
    

    This prints the letters a b c d all in different colors:

    echo -e "\x1B[0;93m a \x1B[0m b \x1B[0;92m c \x1B[0;93m d \x1B[0;94m"
    

    For loop:

    for (( i = 0; i < 17; i++ )); 
    do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; 
    done
    

    enter image description here

    0 讨论(0)
  • 2020-11-22 04:54

    I found Shakiba Moshiri's awesome answer while I was looking info on that topic… then I had an idea… and it ended up in a quite nice function extremely easy to use

    0 讨论(0)
  • 2020-11-22 04:57

    My favourite answer so far is coloredEcho.

    Just to post another option, you can check out this little tool xcol

    https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/

    you use it just like grep, and it will colorize its stdin with a different color for each argument, for instance

    sudo netstat -putan | xcol httpd sshd dnsmasq pulseaudio conky tor Telegram firefox "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" ":[[:digit:]]+" "tcp." "udp." LISTEN ESTABLISHED TIME_WAIT
    

    Note that it accepts any regular expression that sed will accept.

    This tool uses the following definitions

    #normal=$(tput sgr0)                      # normal text
    normal=$'\e[0m'                           # (works better sometimes)
    bold=$(tput bold)                         # make colors bold/bright
    red="$bold$(tput setaf 1)"                # bright red text
    green=$(tput setaf 2)                     # dim green text
    fawn=$(tput setaf 3); beige="$fawn"       # dark yellow text
    yellow="$bold$fawn"                       # bright yellow text
    darkblue=$(tput setaf 4)                  # dim blue text
    blue="$bold$darkblue"                     # bright blue text
    purple=$(tput setaf 5); magenta="$purple" # magenta text
    pink="$bold$purple"                       # bright magenta text
    darkcyan=$(tput setaf 6)                  # dim cyan text
    cyan="$bold$darkcyan"                     # bright cyan text
    gray=$(tput setaf 7)                      # dim white text
    darkgray="$bold"$(tput setaf 0)           # bold black = dark gray text
    white="$bold$gray"                        # bright white text
    

    I use these variables in my scripts like so

    echo "${red}hello ${yellow}this is ${green}coloured${normal}"
    
    0 讨论(0)
  • 2020-11-22 04:59

    You can use these ANSI escape codes:

    Black        0;30     Dark Gray     1;30
    Red          0;31     Light Red     1;31
    Green        0;32     Light Green   1;32
    Brown/Orange 0;33     Yellow        1;33
    Blue         0;34     Light Blue    1;34
    Purple       0;35     Light Purple  1;35
    Cyan         0;36     Light Cyan    1;36
    Light Gray   0;37     White         1;37
    

    And then use them like this in your script:

    #    .---------- constant part!
    #    vvvv vvvv-- the code from above
    RED='\033[0;31m'
    NC='\033[0m' # No Color
    printf "I ${RED}love${NC} Stack Overflow\n"
    

    which prints love in red.

    From @james-lim's comment, if you are using the echo command, be sure to use the -e flag to allow backslash escapes.

    # Continued from above example
    echo -e "I ${RED}love${NC} Stack Overflow"
    

    (don't add "\n" when using echo unless you want to add an additional empty line)

    0 讨论(0)
  • 2020-11-22 04:59

    Here is the simplest and readable solution. With bashj (https://sourceforge.net/projects/bashj/), you would simply choose one of these lines:

    #!/usr/bin/bash
    
    W="Hello world!"
    echo $W
    
    R=130
    G=60
    B=190
    
    echo u.colored($R,$G,$B,$W)
    
    echo u.colored(255,127,0,$W)
    echo u.red($W)
    echo u.bold($W)
    echo u.italic($W)
    
    Y=u.yellow($W)
    echo $Y
    echo u.bold($Y)
    

    256x256x256 colors are available if you have the color support in your terminal application.

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

    You should definitely use tput over raw ANSI control sequences.

    Because there's a large number of different terminal control languages, usually a system has an intermediate communication layer. The real codes are looked up in a database for the currently detected terminal type and you give standardized requests to an API or (from the shell) to a command.

    One of these commands is tput . tput accepts a set of acronyms called capability names and any parameters, if appropriate, then looks up the correct escape sequences for the detected terminal in the terminfo database and prints the correct codes (the terminal hopefully understands).

    from http://wiki.bash-hackers.org/scripting/terminalcodes

    That said, I wrote a small helper library called bash-tint, which adds another layer on top of tput, making it even simpler to use (imho):

    Example: tint "white(Cyan(T)Magenta(I)Yellow(N)Black(T)) is bold(really) easy to use."

    Would give the following result:

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