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

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


当前回答

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

首先使用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年标准具有以下提到的语义)。

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

其他回答

灵感来自@nachoparker的回答,我的.bashrc中有这个:

#### colours
source xcol.sh

### tput foreground
export tpfn=$'\e[0m' # normal
export tpfb=$(tput bold)

## normal colours
export tpf0=$(tput setaf 0) # black
export tpf1=$(tput setaf 1) # red
export tpf2=$(tput setaf 2) # green
export tpf3=$(tput setaf 3) # yellow
export tpf4=$(tput setaf 4) # blue
export tpf5=$(tput setaf 5) # magenta
export tpf6=$(tput setaf 6) # cyan
export tpf7=$(tput setaf 7) # white
# echo "${tpf0}black ${tpf1}red ${tpf2}green ${tpf3}yellow ${tpf4}blue ${tpf5}magenta ${tpf6}cyan ${tpf7}white${tpfn}"

## bold colours
export tpf0b="$tpfb$tpf0" # bold black
export tpf1b="$tpfb$tpf1" # bold red
export tpf2b="$tpfb$tpf2" # bold green
export tpf3b="$tpfb$tpf3" # bold yellow
export tpf4b="$tpfb$tpf4" # bold blue
export tpf5b="$tpfb$tpf5" # bold magenta
export tpf6b="$tpfb$tpf6" # bold cyan
export tpf7b="$tpfb$tpf7" # bold white
# echo "${tpf0b}black ${tpf1b}red ${tpf2b}green ${tpf3b}yellow ${tpf4b}blue ${tpf5b}magenta ${tpf6b}cyan ${tpf7b}white${tpfn}"

出口允许我使用这些tpf。。在Bash脚本中。

您可以“组合”颜色和文本模式。

#!/bin/bash

echo red text / black background \(Reverse\)
echo "\033[31;7mHello world\e[0m";
echo -e "\033[31;7mHello world\e[0m";
echo

echo yellow text / red background
echo "\033[32;41mHello world\e[0m";
echo -e "\033[32;41mHello world\e[0m";
echo "\033[0;32;41mHello world\e[0m";
echo -e "\033[0;32;41mHello world\e[0m";
echo

echo yellow BOLD text / red background
echo "\033[1;32;41mHello world\e[0m";
echo -e "\033[1;32;41mHello world\e[0m";
echo

echo yellow BOLD text underline / red background
echo "\033[1;4;32;41mHello world\e[0m";
echo -e "\033[1;4;32;41mHello world\e[0m";
echo "\033[1;32;4;41mHello world\e[0m";
echo -e "\033[1;32;4;41mHello world\e[0m";
echo "\033[4;32;41;1mHello world\e[0m";
echo -e "\033[4;32;41;1mHello world\e[0m";
echo

我们可以为文本和背景使用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)_参数

我写了swag就是为了实现这一点。

你可以这样做

pip install swag

现在,您可以通过以下方式将所有转义命令作为txt文件安装到给定目标:

swag install -d <colorsdir>

或者更容易通过:

swag install

这将把颜色安装到~/.colors。

您可以这样使用它们:

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

或者这样,我觉得更有趣:

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

在ascinema上查看!

我对托拜厄斯回答的重复:

# Color
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
NC='\033[0m' # No Color

function red {
    printf "${RED}$@${NC}\n"
}

function green {
    printf "${GREEN}$@${NC}\n"
}

function yellow {
    printf "${YELLOW}$@${NC}\n"
}