尚方宝剑之说
你可以用最棒的tput命令(建议在伊格纳西奥的回答)为各种事物制作终端控制代码。使用专一tput稍后将讨论子命令。直接打电话tput作为一系列命令的一部分:tput setaf 1; echo "this is red text"使用;而不是&&所以如果tput文本仍然显示的错误。壳变量另一种选择是使用shell变量:red=`tput setaf 1`green=`tput setaf 2`reset=`tput sgr0`echo "${red}red text ${green}green text${reset}"tput产生被终端解释为具有特殊意义的字符序列。他们不会被展示出来。请注意,它们仍然可以保存到文件中,或者由终端以外的程序作为输入处理。命令替换插入可能更方便tput的输出直接进入echo字符串使用命令替换:echo "$(tput setaf 1)Red text $(tput setab 7)and white background$(tput sgr 0)"前景和背景颜色命令tput setab [1-7] # Set the background colour using ANSI escapetput 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 modetput dim # Select dim (half-bright) modetput smul # Enable underline modetput rmul
# Disable underline modetput rev # Turn on reverse video modetput smso # Enter standout (bold) modetput 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 uptput ll # Move to last line, first column (if no cup)tput sc
# Save the cursor positiontput rc # Restore the cursor positiontput lines # Output the number of lines of the terminaltput cols
# Output the number of columns of the terminal清除和插入命令tput ech N # Erase N characterstput clear # Clear screen and move the cursor to 0,0tput el 1
# Clear to beginning of linetput el # Clear to end of linetput ed # Clear to end of screentput 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 defaulttput bel # Play a bell带着车窗摇摇晃晃,bel命令使终端抖动一秒钟,以引起用户的注意。剧本tput接受每行包含一个命令的脚本,这些命令是按照前面的顺序执行的。tput出口。通过回显多行字符串并对其进行管道处理,可以避免临时文件:echo -e "setf 7\nsetb 1" | tput -S # set fg white and bg red另见看见man 1 tput看见man 5 terminfo有关这些选项的完整命令列表和更多详细信息。(对应tput命令列在Cap-name)从第81行开始的巨型表的列。)