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

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


当前回答

使用tput计算颜色代码。避免使用ANSI转义码(例如,\e[31;1m表示红色),因为它不易移植。例如,OS X上的Bash不支持它。

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"

其他回答

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

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

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

为了进一步解释这个答案,对于我们这些懒惰的人来说:

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"

灵感来自@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脚本中。

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

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