我正在尝试使用echo命令在终端中打印文本。

我想用红色打印文本。我该怎么做?


当前回答

这是颜色开关\033[。请参阅历史记录。

颜色代码类似于1;32(浅绿色),0;34(蓝色),1;34(浅蓝色)等。

我们使用颜色开关\033[和0m(无颜色代码)终止颜色序列。就像标记语言中的打开和关闭选项卡一样。

  SWITCH="\033["
  NORMAL="${SWITCH}0m"
  YELLOW="${SWITCH}1;33m"
  echo "${YELLOW}hello, yellow${NORMAL}"

简单的彩色回声功能解决方案:

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!"

其他回答

这些代码在我的Ubuntu盒子上运行:

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"

这将以不同的颜色打印字母a、b、c、d:

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

对于循环:

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

我们可以为文本和背景使用24位RGB真彩色!

 ESC[38;2;⟨r⟩;⟨g⟩;⟨b⟩m  /*Foreground color*/
 ESC[48;2;⟨r⟩;⟨g⟩;⟨b⟩m  /*Background color*/

红色文本和结束标记示例:

 echo -e "\e[38;2;255;0;0mHello world\e[0m"

发电机:text.addEventListener(“输入”,更新)back.addEventListener(“输入”,更新)函数更新(){让a=text.value.substr(1).match(/.{1,2}/g)设b=back.value.substr(1).match(/.{1,2}/g)out1.textContent=“echo-e\”\\“+`033[38;2;${parseInt(a[0],16)};${parseInt(a[1],16)};${parseInt(a[2],16)}mHello\”`out2.textContent=“echo-e\”\\“+`033[48;2;${parseInt(b[0],16)};${parseInt(b[1],16)};${parseInt(b[2],16)}mWorld!\”`}div{padding:1rem;字体大小:更大}文本颜色:<input type=“COLOR”id=“TEXT”value=“#22333”><br><div id=“out1”></div>BACK COLOR:<input type=“COLOR”id=“BACK”value=“#FFFF00”><br><div id=“out2”>

24位:作为具有16至24位颜色的“真彩色”图形卡Xterm、KDE的Konsole以及所有libvte都变得很常见基于的终端(包括GNOME终端)支持24位前景色和背景色设置https://en.wikipedia.org/wiki/ANSI_escape_code#24-钻头,钻头

在我的脚本中使用它安全吗?

对8位和16位终端将只显示可用调色板范围内的回退颜色,保持最佳对比度,无破损!


此外,没有人注意到ANSI代码7反转视频的有用性。

通过交换前景和背景颜色,它可以在任何终端方案颜色、黑色或白色背景或其他幻想调色板上保持可读性。

例如,对于随处可见的红色背景:

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

这是更改终端内置方案时的外观:

这是用于gif的循环脚本。

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

看见https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Redition)_参数

我应该使用tput,而不是硬编码特定于当前终端的转义码。

这是我最喜欢的演示脚本:

#!/bin/bash

tput init

end=$(( $(tput colors)-1 ))
w=8
for c in $(seq 0 $end); do
    eval "$(printf "tput setaf %3s   " "$c")"; echo -n "$_"
    [[ $c -ge $(( w*2 )) ]] && offset=2 || offset=0
    [[ $(((c+offset) % (w-offset))) -eq $(((w-offset)-1)) ]] && echo
done

tput init

这是我过去看到的所有组合,并决定哪一个读起来很酷:

for (( i = 0; i < 8; i++ )); do
    for (( j = 0; j < 8; j++ )); do
        printf "$(tput setab $i)$(tput setaf $j)(b=$i, f=$j)$(tput sgr0)\n"
    done
done

这个问题已经被一次又一次地回答了:-)但为什么不呢。

首先使用tput在现代环境中比通过echo-E手动注入ASCII代码更容易移植

下面是一个快速的bash函数:

 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"
  }

现在您可以使用:

 say @b@green[[Success]] 

得到:

关于tput可移植性的说明

1986年9月首次上传tput(1)源代码

tput(1)在20世纪90年代已在X/Open curses语义中可用(1997年标准具有以下提到的语义)。

因此,它(相当)无处不在。