小编典典

如何在 Linux 中更改 echo 的输出颜色

linux

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

我想以红色打印文本。我怎样才能做到这一点?


阅读 332

收藏
2022-02-08

共2个答案

小编典典

可以使用这些ANSI 转义码

Black        0;30     Dark Gray     1;30
Red          0;31     Light Red     1;31
Green        0;32     Light Green   1;32
Brown/Orange 0;33     Yellow        1;33
Blue         0;34     Light Blue    1;34
Purple       0;35     Light Purple  1;35
Cyan         0;36     Light Cyan    1;36
Light Gray   0;37     White         1;37

然后在你的脚本中像这样使用它们:

#    .---------- constant part!
#    vvvv vvvv-- the code from above
RED='\033[0;31m'
NC='\033[0m' # No Color
printf "I ${RED}love${NC} Stack Overflow\n"

以红色打印love

根据评论,如果您使用该echo命令,请务必使用 -e 标志来允许反斜杠转义

# Continued from above example
echo -e "I ${RED}love${NC} Stack Overflow"

"\n"(使用时请勿添加,echo除非您想添加额外的空行)

2022-02-08
小编典典

您可以使用 awesometput命令中建议为各种事物生成终端控制代码。


用法

具体tput的子命令将在后面讨论。

直接的

tput作为命令序列的一部分调用:

tput setaf 1; echo "this is red text"

如果文本仍然显示错误,请使用;而不是。&&``tput

shell变量

另一种选择是使用 shell 变量:

red=`tput setaf 1`
green=`tput setaf 2`
reset=`tput sgr0`
echo "${red}red text ${green}green text${reset}"

tput产生被终端解释为具有特殊含义的字符序列。他们不会自己显示出来。请注意,它们仍然可以保存到文件中或由终端以外的程序作为输入处理。

命令替换

使用命令替换tput将’s 的输出直接插入echo字符串可能更方便:

echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"

例子

上面的命令在 Ubuntu 上产生这个:

彩色终端文字截图


前景色和背景色命令

tput setab [1-7] # Set the background colour using ANSI escape
tput setaf [1-7] # Set the foreground colour using ANSI escape

颜色如下:

Num  Colour    #define         R G B

0    black     COLOR_BLACK     0,0,0
1    red       COLOR_RED       1,0,0
2    green     COLOR_GREEN     0,1,0
3    yellow    COLOR_YELLOW    1,1,0
4    blue      COLOR_BLUE      0,0,1
5    magenta   COLOR_MAGENTA   1,0,1
6    cyan      COLOR_CYAN      0,1,1
7    white     COLOR_WHITE     1,1,1

还有非 ANSI 版本的颜色设置函数(setb代替setab, 和setf代替setaf)使用不同的数字,这里没有给出。

文本模式命令

tput bold    # Select bold mode
tput dim     # Select dim (half-bright) mode
tput smul    # Enable underline mode
tput rmul    # Disable underline mode
tput rev     # Turn on reverse video mode
tput smso    # Enter standout (bold) mode
tput rmso    # Exit standout mode

光标移动命令

tput cup Y X # Move cursor to screen postion X,Y (top left is 0,0)
tput cuf N   # Move N characters forward (right)
tput cub N   # Move N characters back (left)
tput cuu N   # Move N lines up
tput ll      # Move to last line, first column (if no cup)
tput sc      # Save the cursor position
tput rc      # Restore the cursor position
tput lines   # Output the number of lines of the terminal
tput cols    # Output the number of columns of the terminal

清除和插入命令

tput ech N   # Erase N characters
tput clear   # Clear screen and move the cursor to 0,0
tput el 1    # Clear to beginning of line
tput el      # Clear to end of line
tput ed      # Clear to end of screen
tput ich N   # Insert N characters (moves rest of line forward!)
tput il N    # Insert N lines

其他命令

tput sgr0    # Reset text format to the terminal's default
tput bel     # Play a bell

使用compiz wobbly windows,该bel命令使终端摇晃一秒钟以引起用户的注意。


脚本

tput接受每行包含一个命令的脚本,这些命令在tput退出前按顺序执行。

通过回显多行字符串和管道来避免临时文件:

echo -e "setf 7\nsetb 1" | tput -S  # set fg white and bg red
2022-02-08