How to change the output color of echo in Linux

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

    If you are using zsh or bash

    black() {
        echo -e "\e[30m${1}\e[0m"
    }
    
    red() {
        echo -e "\e[31m${1}\e[0m"
    }
    
    green() {
        echo -e "\e[32m${1}\e[0m"
    }
    
    yellow() {
        echo -e "\e[33m${1}\e[0m"
    }
    
    blue() {
        echo -e "\e[34m${1}\e[0m"
    }
    
    magenta() {
        echo -e "\e[35m${1}\e[0m"
    }
    
    cyan() {
        echo -e "\e[36m${1}\e[0m"
    }
    
    gray() {
        echo -e "\e[90m${1}\e[0m"
    }
    
    black 'BLACK'
    red 'RED'
    green 'GREEN'
    yellow 'YELLOW'
    blue 'BLUE'
    magenta 'MAGENTA'
    cyan 'CYAN'
    gray 'GRAY'
    

    Try online

提交回复
热议问题