linux - [Solved-6 Answers] How to change the output color of echo in Linux - ubuntu - red hat - debian - linux server - linux pc



Linux - Problem :

How to change the output color of echo in Linux ?

Linux - Solution 1:

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
click below button to copy the code. By - Linux tutorial - team

And then use them like this in your script:

#    .---------- constant part!
#    vvvv vvvv-- the code from above
BLUE='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${BLUE}love${NC} Wikitechy\n"
click below button to copy the code. By - Linux tutorial - team
# Continued from above example
echo -e "I ${BLUE}love${NC} wikitechy"
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 2:

Use tput with the setaf capability and a parameter of 1.

echo "$(tput setaf 1)Hello, world$(tput sgr0)"
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 3:

echo -e "\033[31m Hello World"
click below button to copy the code. By - Linux tutorial - team

The [31m controls the text color:

  • 30-37 sets foreground color
  • 40-47 sets background color

Linux - Solution 4:

This is the color switch \033[.

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}"
click below button to copy the code. By - Linux tutorial - team

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!"
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 5:

A neat way to change color only for one echo is to define such function:

function coloredEcho(){
    local exp=$1;
    local color=$2;
    if ! [[ $color =~ '^[0-9]$' ]] ; then
       case $(echo $color | tr '[:upper:]' '[:lower:]') in
        black) color=0 ;;
        red) color=1 ;;
        green) color=2 ;;
        yellow) color=3 ;;
        blue) color=4 ;;
        magenta) color=5 ;;
        cyan) color=6 ;;
        white|*) color=7 ;; # white or invalid color
       esac
    fi
    tput setaf $color;
    echo $exp;
    tput sgr0;
}
click below button to copy the code. By - Linux tutorial - team

Usage:

coloredEcho "This text is green" green
click below button to copy the code. By - Linux tutorial - team

Or you could directly use color codes mentioned

coloredEcho "This text is green" 2
click below button to copy the code. By - Linux tutorial - team

Linux - Solution 6:

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"
click below button to copy the code. By - Linux tutorial - team

Related Searches to - linux - linux tutorial - How to change the output color of echo in Linux