How to change the output color of echo in Linux

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

    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"
    

提交回复
热议问题