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:36

Nobody noticed the usefulness of the ANSI code 7 reversed video.

It stay readable on any terminal schemes colors, black or white backgrounds, or other fancies palettes, by swapping foreground and background colors.

Example, for a red background that works everywhere:

echo -e "\033[31;7mHello world\e[0m";

This is how it looks when changing the terminal built-in schemes:

enter image description here

This is the loop script used for the gif.

for i in {30..49};do echo -e "\033[$i;7mReversed color code $i\e[0m Hello world!";done

See https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters

查看更多
旧人旧事旧时光
3楼-- · 2018-12-31 17:36
red='\e[0;31m'
NC='\e[0m' # No Color
echo -e "${red}Hello Stackoverflow${NC}"

This answer correct, except that the call to colors should not be inside the quotes.

echo -e ${red}"Hello Stackoverflow"${NC}

Should do the trick.

查看更多
长期被迫恋爱
4楼-- · 2018-12-31 17:38

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!"
查看更多
余欢
5楼-- · 2018-12-31 17:38

These codes work on my Ubuntu box:

enter image description here

echo -e "\x1B[31m foobar \x1B[0m"
echo -e "\x1B[32m foobar \x1B[0m"
echo -e "\x1B[96m foobar \x1B[0m"
echo -e "\x1B[01;96m foobar \x1B[0m"
echo -e "\x1B[01;95m foobar \x1B[0m"
echo -e "\x1B[01;94m foobar \x1B[0m"
echo -e "\x1B[01;93m foobar \x1B[0m"
echo -e "\x1B[01;91m foobar \x1B[0m"
echo -e "\x1B[01;90m foobar \x1B[0m"
echo -e "\x1B[01;89m foobar \x1B[0m"
echo -e "\x1B[01;36m foobar \x1B[0m"

This prints the letters a b c d all in different colors:

echo -e "\x1B[0;93m a \x1B[0m b \x1B[0;92m c \x1B[0;93m d \x1B[0;94m"

For loop:

for (( i = 0; i < 17; i++ )); 
do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; 
done

enter image description here

查看更多
皆成旧梦
6楼-- · 2018-12-31 17:39

I've written swag to achieve just that.

You can just do

pip install swag

Now you can install all the the escape commands as txt files to a given destination via:

swag install -d <colorsdir>

Or even easier via:

swag install

Which will install the colors to ~/.colors.

Either you use them like this:

echo $(cat ~/.colors/blue.txt) This will be blue

Or this way, which I find actually more interesting:

swag print -c red -t underline "I will turn red and be underlined"

Check it out on asciinema!

查看更多
其实,你不懂
7楼-- · 2018-12-31 17:39

Here is the simplest and readable solution. With bashj (https://sourceforge.net/projects/bashj/), you would simply choose one of these lines:

#!/usr/bin/bash

W="Hello world!"
echo $W

R=130
G=60
B=190

echo u.colored($R,$G,$B,$W)

echo u.colored(255,127,0,$W)
echo u.red($W)
echo u.bold($W)
echo u.italic($W)

Y=u.yellow($W)
echo $Y
echo u.bold($Y)

256x256x256 colors are available if you have the color support in your terminal application.

查看更多
登录 后发表回答