How to change the output color of echo in Linux

2018-12-31 17:12发布

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?

23条回答
无色无味的生活
2楼-- · 2018-12-31 17:29

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"
查看更多
像晚风撩人
3楼-- · 2018-12-31 17:30

Here is a simple little script, I put together recently, that will colorize any piped input instead of using "Toilet".

File: color.bsh

#!/usr/bin/env bash 

## A.M.Danischewski 2015+(c) Free - for (all (uses and 
## modifications)) - except you must keep this notice intact. 

declare INPUT_TXT=""
declare    ADD_LF="\n" 
declare -i DONE=0
declare -r COLOR_NUMBER="${1:-247}"
declare -r ASCII_FG="\\033[38;05;"
declare -r COLOR_OUT="${ASCII_FG}${COLOR_NUMBER}m"

function show_colors() { 
   ## perhaps will add bg 48 to first loop eventually 
 for fgbg in 38; do for color in {0..256} ; do 
 echo -en "\\033[${fgbg};5;${color}m ${color}\t\\033[0m"; 
 (($((${color}+1))%10==0)) && echo; done; echo; done
} 

if [[ ! $# -eq 1 || ${1} =~ ^-. ]]; then 
  show_colors 
  echo " Usage: ${0##*/} <color fg>" 
  echo "  E.g. echo \"Hello world!\" | figlet | ${0##*/} 54" 
else  
 while IFS= read -r PIPED_INPUT || { DONE=1; ADD_LF=""; }; do 
  PIPED_INPUT=$(sed 's#\\#\\\\#g' <<< "${PIPED_INPUT}")
  INPUT_TXT="${INPUT_TXT}${PIPED_INPUT}${ADD_LF}"
  ((${DONE})) && break; 
 done
 echo -en "${COLOR_OUT}${INPUT_TXT}\\033[00m"
fi 

Then call it with color red (196):
$> echo "text you want colored red" | color.bsh 196

查看更多
宁负流年不负卿
4楼-- · 2018-12-31 17:32

Just as something a little out there, passing it through grep will highlight it as red (but only red). You can also use named pipes so your string is nearer to the end of the line:

 grep '.*' --color=always <(echo "foobar")
查看更多
心情的温度
5楼-- · 2018-12-31 17:33

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

echo "$(tput setaf 1)Hello, world$(tput sgr0)"
查看更多
余生无你
6楼-- · 2018-12-31 17:34

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"
查看更多
长期被迫恋爱
7楼-- · 2018-12-31 17:35

This question has been answered over and over again :-) but why not.

First using tput is more portable in modern environments than manually injecting ASCII codes through echo -E

Here's a quick bash function:

 say() {
     echo "$@" | sed \
             -e "s/\(\(@\(red\|green\|yellow\|blue\|magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\4@reset/g" \
             -e "s/@red/$(tput setaf 1)/g" \
             -e "s/@green/$(tput setaf 2)/g" \
             -e "s/@yellow/$(tput setaf 3)/g" \
             -e "s/@blue/$(tput setaf 4)/g" \
             -e "s/@magenta/$(tput setaf 5)/g" \
             -e "s/@cyan/$(tput setaf 6)/g" \
             -e "s/@white/$(tput setaf 7)/g" \
             -e "s/@reset/$(tput sgr0)/g" \
             -e "s/@b/$(tput bold)/g" \
             -e "s/@u/$(tput sgr 0 1)/g"
  }

Now you can use:

 say @b@green[[Success]] 

to get:

Bold-Green Success

Notes on portability of tput

First time tput(1) source code was uploaded in September 1986

tput(1) has been available in X/Open curses semantics in 1990s (1997 standard has the semantics mentioned below).

So, it's (quite) ubiquitous.

查看更多
登录 后发表回答