How to change the output color of echo in Linux

后端 未结 29 3551
刺人心
刺人心 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:48

    This is the color switch \033[. See history.

    Color codes are like 1;32 (Light Green), 0;34 (Blue), 1;34 (Light Blue), etc.

    We terminate color sequences with a color switch \033[ and 0m, the no-color code. Just like opening and closing tabs in a markup language.

      SWITCH="\033["
      NORMAL="${SWITCH}0m"
      YELLOW="${SWITCH}1;33m"
      echo "${YELLOW}hello, yellow${NORMAL}"
    

    Simple color echo function solution:

    cecho() {
      local code="\033["
      case "$1" in
        black  | bk) color="${code}0;30m";;
        red    |  r) color="${code}1;31m";;
        green  |  g) color="${code}1;32m";;
        yellow |  y) color="${code}1;33m";;
        blue   |  b) color="${code}1;34m";;
        purple |  p) color="${code}1;35m";;
        cyan   |  c) color="${code}1;36m";;
        gray   | gr) color="${code}0;37m";;
        *) local text="$1"
      esac
      [ -z "$text" ] && local text="$color$2${code}0m"
      echo "$text"
    }
    
    cecho "Normal"
    cecho y "Yellow!"
    
    0 讨论(0)
  • 2020-11-22 04:48

    Use tput to calculate color codes. Avoid using the ANSI escape code (e.g. \E[31;1m for red) because it's less portable. Bash on OS X, for example, does not support it.

    BLACK=`tput setaf 0`
    RED=`tput setaf 1`
    GREEN=`tput setaf 2`
    YELLOW=`tput setaf 3`
    BLUE=`tput setaf 4`
    MAGENTA=`tput setaf 5`
    CYAN=`tput setaf 6`
    WHITE=`tput setaf 7`
    
    BOLD=`tput bold`
    RESET=`tput sgr0`
    
    echo -e "hello ${RED}some red text${RESET} world"
    
    0 讨论(0)
  • 2020-11-22 04:51

    For readability

    If you want to improve the readability of the code, you can echo the string first then add the color later by using sed:

    echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/' 
    
    0 讨论(0)
  • 2020-11-22 04:51

    To expand on this answer, for the lazy of us:

    function echocolor() { # $1 = string
        COLOR='\033[1;33m'
        NC='\033[0m'
        printf "${COLOR}$1${NC}\n"
    }
    
    echo "This won't be colored"
    echocolor "This will be colorful"
    
    0 讨论(0)
  • 2020-11-22 04:51

    Inspired by @nachoparker's answer, I have this in my .bashrc:

    #### colours
    source xcol.sh
    
    ### tput foreground
    export tpfn=$'\e[0m' # normal
    export tpfb=$(tput bold)
    
    ## normal colours
    export tpf0=$(tput setaf 0) # black
    export tpf1=$(tput setaf 1) # red
    export tpf2=$(tput setaf 2) # green
    export tpf3=$(tput setaf 3) # yellow
    export tpf4=$(tput setaf 4) # blue
    export tpf5=$(tput setaf 5) # magenta
    export tpf6=$(tput setaf 6) # cyan
    export tpf7=$(tput setaf 7) # white
    # echo "${tpf0}black ${tpf1}red ${tpf2}green ${tpf3}yellow ${tpf4}blue ${tpf5}magenta ${tpf6}cyan ${tpf7}white${tpfn}"
    
    ## bold colours
    export tpf0b="$tpfb$tpf0" # bold black
    export tpf1b="$tpfb$tpf1" # bold red
    export tpf2b="$tpfb$tpf2" # bold green
    export tpf3b="$tpfb$tpf3" # bold yellow
    export tpf4b="$tpfb$tpf4" # bold blue
    export tpf5b="$tpfb$tpf5" # bold magenta
    export tpf6b="$tpfb$tpf6" # bold cyan
    export tpf7b="$tpfb$tpf7" # bold white
    # echo "${tpf0b}black ${tpf1b}red ${tpf2b}green ${tpf3b}yellow ${tpf4b}blue ${tpf5b}magenta ${tpf6b}cyan ${tpf7b}white${tpfn}"
    

    The export allows me to use those tpf.. in Bash scripts.

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

    I instead of hard coding escape codes that are specific to your current terminal, you should use tput.

    This is my favorite demo script:

    #!/bin/bash
    
    tput init
    
    end=$(( $(tput colors)-1 ))
    w=8
    for c in $(seq 0 $end); do
        eval "$(printf "tput setaf %3s   " "$c")"; echo -n "$_"
        [[ $c -ge $(( w*2 )) ]] && offset=2 || offset=0
        [[ $(((c+offset) % (w-offset))) -eq $(((w-offset)-1)) ]] && echo
    done
    
    tput init
    

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