我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
我正在尝试使用echo命令在终端中打印文本。
我想用红色打印文本。我该怎么做?
当前回答
这是颜色开关\033[。请参阅历史记录。
颜色代码类似于1;32(浅绿色),0;34(蓝色),1;34(浅蓝色)等。
我们使用颜色开关\033[和0m(无颜色代码)终止颜色序列。就像标记语言中的打开和关闭选项卡一样。
SWITCH="\033["
NORMAL="${SWITCH}0m"
YELLOW="${SWITCH}1;33m"
echo "${YELLOW}hello, yellow${NORMAL}"
简单的彩色回声功能解决方案:
cecho() {
local code="\033["
case "$1" in
black | bk) color="${code}0;30m";;
red | r) color="${code}1;31m";;
green | g) color="${code}1;32m";;
yellow | y) color="${code}1;33m";;
blue | b) color="${code}1;34m";;
purple | p) color="${code}1;35m";;
cyan | c) color="${code}1;36m";;
gray | gr) color="${code}0;37m";;
*) local text="$1"
esac
[ -z "$text" ] && local text="$color$2${code}0m"
echo "$text"
}
cecho "Normal"
cecho y "Yellow!"
其他回答
只为一个回声改变颜色的一个简单方法是定义这样的函数:
function coloredEcho(){
local exp=$1;
local color=$2;
if ! [[ $color =~ '^[0-9]$' ]] ; then
case $(echo $color | tr '[:upper:]' '[:lower:]') in
black) color=0 ;;
red) color=1 ;;
green) color=2 ;;
yellow) color=3 ;;
blue) color=4 ;;
magenta) color=5 ;;
cyan) color=6 ;;
white|*) color=7 ;; # white or invalid color
esac
fi
tput setaf $color;
echo $exp;
tput sgr0;
}
用法:
coloredEcho "This text is green" green
或者你可以直接使用德鲁回答中提到的颜色代码:
coloredEcho "This text is green" 2
您可以使用很棒的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列中。)
red='\e[0;31m'
NC='\e[0m' # No Color
echo -e "${red}Hello Stackoverflow${NC}"
这个答案是正确的,只是对颜色的调用不应该在引号内。
echo -e ${red}"Hello Stackoverflow"${NC}
应该会成功的。
为了进一步解释这个答案,对于我们这些懒惰的人来说:
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"
要显示具有不同颜色的消息输出,可以执行以下操作:
echo -e "\033[31;1mYour Message\033[0m"
-黑色0;30深灰色1;30-红色0;31浅红色1;31-绿色0;32浅绿色1;32-棕色/橙色0;33黄色1;33-蓝色0;34浅蓝色1;34-紫色0;35浅紫色1;35-青色0;36浅青色1;36-浅灰色0;37白色1;37