我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
当前回答
您可以使用很棒的tput命令(在Ignacio的回答中建议)来为各种事情生成终端控制代码。
用法
稍后将讨论特定的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的输出直接插入回显字符串可能更方便:
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摇摆窗口,bel命令使终端摇摆一秒钟以吸引用户的注意力。
脚本
tput接受每行包含一个命令的脚本,这些脚本在tput退出之前按顺序执行。
通过回显多行字符串和管道来避免临时文件:
echo -e "setf 7\nsetb 1" | tput -S # set fg white and bg red
另请参见
见man 1 tput有关命令的完整列表以及有关这些选项的更多详细信息,请参阅man 5 terminfo。(相应的tput命令列在从第81行开始的巨大表的Cap-name列中。)
其他回答
echo -e "\033[31m Hello World"
[31m控制文本颜色:
30-37套前景色40-47套背景色
这里可以找到更完整的颜色代码列表。
最好将字符串末尾的文本颜色重置回\033[0m。
这是最简单、易读的解决方案。与bashj(https://sourceforge.net/projects/bashj/),您只需选择以下行之一:
#!/usr/bin/bash
W="Hello world!"
echo $W
R=130
G=60
B=190
echo u.colored($R,$G,$B,$W)
echo u.colored(255,127,0,$W)
echo u.red($W)
echo u.bold($W)
echo u.italic($W)
Y=u.yellow($W)
echo $Y
echo u.bold($Y)
如果您的终端应用程序支持颜色,则可以使用256x256x256色。
为了可读性
如果要提高代码的可读性,可以先回显字符串,然后使用sed添加颜色:
echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/'
您绝对应该在原始ANSI控制序列上使用tput。
因为有大量不同的终端控制语言,通常系统有一个中间通信层。在数据库中查找当前检测到的真实代码终端类型,并向API或(来自shell)转换为命令。其中一个命令是tput。tput接受一组名为功能名称和任何参数(如果合适),然后查找终端中检测到的终端的正确转义序列数据库并打印正确的代码(希望终端理解)。
从…起http://wiki.bash-hackers.org/scripting/terminalcodes
也就是说,我编写了一个名为bash tint的小助手库,它在tput之上添加了另一层,使其使用起来更加简单(imho):
例子:tint“白色(青色(T)洋红色(I)黄色(N)黑色(T))粗体(真的)易于使用。”
将给出以下结果:
我用这个做彩色印刷
#!/bin/bash
#--------------------------------------------------------------------+
#Color picker, usage: printf $BLD$CUR$RED$BBLU'Hello World!'$DEF |
#-------------------------+--------------------------------+---------+
# Text color | Background color | |
#-----------+-------------+--------------+-----------------+ |
# Base color|Lighter shade| Base color | Lighter shade | |
#-----------+-------------+--------------+-----------------+ |
BLK='\e[30m'; blk='\e[90m'; BBLK='\e[40m'; bblk='\e[100m' #| Black |
RED='\e[31m'; red='\e[91m'; BRED='\e[41m'; bred='\e[101m' #| Red |
GRN='\e[32m'; grn='\e[92m'; BGRN='\e[42m'; bgrn='\e[102m' #| Green |
YLW='\e[33m'; ylw='\e[93m'; BYLW='\e[43m'; bylw='\e[103m' #| Yellow |
BLU='\e[34m'; blu='\e[94m'; BBLU='\e[44m'; bblu='\e[104m' #| Blue |
MGN='\e[35m'; mgn='\e[95m'; BMGN='\e[45m'; bmgn='\e[105m' #| Magenta |
CYN='\e[36m'; cyn='\e[96m'; BCYN='\e[46m'; bcyn='\e[106m' #| Cyan |
WHT='\e[37m'; wht='\e[97m'; BWHT='\e[47m'; bwht='\e[107m' #| White |
#-------------------------{ Effects }----------------------+---------+
DEF='\e[0m' #Default color and effects |
BLD='\e[1m' #Bold\brighter |
DIM='\e[2m' #Dim\darker |
CUR='\e[3m' #Italic font |
UND='\e[4m' #Underline |
INV='\e[7m' #Inverted |
COF='\e[?25l' #Cursor Off |
CON='\e[?25h' #Cursor On |
#------------------------{ Functions }-------------------------------+
# Text positioning, usage: XY 10 10 'Hello World!' |
XY () { printf "\e[$2;${1}H$3"; } #|
# Print line, usage: line - 10 | line -= 20 | line 'Hello World!' 20 |
line () { printf -v _L %$2s; printf -- "${_L// /$1}"; } #|
# Create sequence like {0..(X-1)} |
que () { printf -v _N %$1s; _N=(${_N// / 1}); printf "${!_N[*]}"; } #|
#--------------------------------------------------------------------+
所有的基本颜色都设置为vars,还有一些有用的函数:XY、line和que。将此脚本源于您的一个脚本,并使用所有颜色vars和函数。