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

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


当前回答

其他的答案已经很好地解释了如何做到这一点。我仍然缺少的是对颜色代码的一个精心安排的概述。维基百科文章“ANSI转义码”对此非常有用。然而,由于颜色通常可以在每个终端中进行配置,并且看起来不同,所以我更希望有一个可以在终端中调用的功能。为此,我创建了以下函数来显示颜色表,并提醒我如何设置它们(排列方式受wiki文章的启发)。例如,您可以将它们加载到.bashrc/.zshrc中,或将它们作为脚本放在某处。

256色

由bash/zsh脚本生成:

function showcolors256() {
    local row col blockrow blockcol red green blue
    local showcolor=_showcolor256_${1:-bg}
    local white="\033[1;37m"
    local reset="\033[0m"

    echo -e "Set foreground color: \\\\033[38;5;${white}NNN${reset}m"
    echo -e "Set background color: \\\\033[48;5;${white}NNN${reset}m"
    echo -e "Reset color & style:  \\\\033[0m"
    echo

    echo 16 standard color codes:
    for row in {0..1}; do
        for col in {0..7}; do
            $showcolor $(( row*8 + col )) $row
        done
        echo
    done
    echo

    echo 6·6·6 RGB color codes:
    for blockrow in {0..2}; do
        for red in {0..5}; do
            for blockcol in {0..1}; do
                green=$(( blockrow*2 + blockcol ))
                for blue in {0..5}; do
                    $showcolor $(( red*36 + green*6 + blue + 16 )) $green
                done
                echo -n "  "
            done
            echo
        done
        echo
    done

    echo 24 grayscale color codes:
    for row in {0..1}; do
        for col in {0..11}; do
            $showcolor $(( row*12 + col + 232 )) $row
        done
        echo
    done
    echo
}

function _showcolor256_fg() {
    local code=$( printf %03d $1 )
    echo -ne "\033[38;5;${code}m"
    echo -nE " $code "
    echo -ne "\033[0m"
}

function _showcolor256_bg() {
    if (( $2 % 2 == 0 )); then
        echo -ne "\033[1;37m"
    else
        echo -ne "\033[0;30m"
    fi
    local code=$( printf %03d $1 )
    echo -ne "\033[48;5;${code}m"
    echo -nE " $code "
    echo -ne "\033[0m"
}

16种颜色

由bash/zsh脚本生成:

function showcolors16() {
    _showcolor "\033[0;30m" "\033[1;30m" "\033[40m" "\033[100m"
    _showcolor "\033[0;31m" "\033[1;31m" "\033[41m" "\033[101m"
    _showcolor "\033[0;32m" "\033[1;32m" "\033[42m" "\033[102m"
    _showcolor "\033[0;33m" "\033[1;33m" "\033[43m" "\033[103m"
    _showcolor "\033[0;34m" "\033[1;34m" "\033[44m" "\033[104m"
    _showcolor "\033[0;35m" "\033[1;35m" "\033[45m" "\033[105m"
    _showcolor "\033[0;36m" "\033[1;36m" "\033[46m" "\033[106m"
    _showcolor "\033[0;37m" "\033[1;37m" "\033[47m" "\033[107m"
}

function _showcolor() {
    for code in $@; do
        echo -ne "$code"
        echo -nE "   $code"
        echo -ne "   \033[0m  "
    done
    echo
}

其他回答

为了可读性

如果要提高代码的可读性,可以先回显字符串,然后使用sed添加颜色:

echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/' 

我写了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上查看!

这些代码在我的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

您绝对应该在原始ANSI控制序列上使用tput。

因为有大量不同的终端控制语言,通常系统有一个中间通信层。在数据库中查找当前检测到的真实代码终端类型,并向API或(来自shell)转换为命令。其中一个命令是tput。tput接受一组名为功能名称和任何参数(如果合适),然后查找终端中检测到的终端的正确转义序列数据库并打印正确的代码(希望终端理解)。

从…起http://wiki.bash-hackers.org/scripting/terminalcodes

也就是说,我编写了一个名为bash tint的小助手库,它在tput之上添加了另一层,使其使用起来更加简单(imho):

例子:tint“白色(青色(T)洋红色(I)黄色(N)黑色(T))粗体(真的)易于使用。”

将给出以下结果:

到目前为止,我最喜欢的答案是彩色回声。

为了发布另一个选项,您可以查看这个小工具xcol

https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/

您可以像grep一样使用它,例如,它会为每个参数用不同的颜色来着色其stdin

sudo netstat -putan | xcol httpd sshd dnsmasq pulseaudio conky tor Telegram firefox "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" ":[[:digit:]]+" "tcp." "udp." LISTEN ESTABLISHED TIME_WAIT

注意,它接受sed将接受的任何正则表达式。

此工具使用以下定义

#normal=$(tput sgr0)                      # normal text
normal=$'\e[0m'                           # (works better sometimes)
bold=$(tput bold)                         # make colors bold/bright
red="$bold$(tput setaf 1)"                # bright red text
green=$(tput setaf 2)                     # dim green text
fawn=$(tput setaf 3); beige="$fawn"       # dark yellow text
yellow="$bold$fawn"                       # bright yellow text
darkblue=$(tput setaf 4)                  # dim blue text
blue="$bold$darkblue"                     # bright blue text
purple=$(tput setaf 5); magenta="$purple" # magenta text
pink="$bold$purple"                       # bright magenta text
darkcyan=$(tput setaf 6)                  # dim cyan text
cyan="$bold$darkcyan"                     # bright cyan text
gray=$(tput setaf 7)                      # dim white text
darkgray="$bold"$(tput setaf 0)           # bold black = dark gray text
white="$bold$gray"                        # bright white text

我像这样在脚本中使用这些变量

echo "${red}hello ${yellow}this is ${green}coloured${normal}"