本文共 30299 字,大约阅读时间需要 100 分钟。
我正在尝试使用命令在终端中打印文本。
我想用红色打印文本。 我怎样才能做到这一点?
您可以使用很棒的tput
命令(在建议)来为各种事物产生终端控制代码。
稍后将讨论特定的tput
子命令。
调用tput
作为命令序列的一部分:
tput setaf 1; echo "this is red text"
使用;
而不是&&
因此如果tput
错误仍然显示文本。
另一种选择是使用外壳变量:
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)"
上面的命令在Ubuntu上生成:
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 B0 black COLOR_BLACK 0,0,01 red COLOR_RED 1,0,02 green COLOR_GREEN 0,1,03 yellow COLOR_YELLOW 1,1,04 blue COLOR_BLUE 0,0,15 magenta COLOR_MAGENTA 1,0,16 cyan COLOR_CYAN 0,1,17 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
tput
命令在Cap-name
81行开始的巨大表的Cap-name
列中列出。) 一种仅对一个echo
更改颜色的巧妙方法是定义以下功能:
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
red='\e[0;31m'NC='\e[0m' # No Colorecho -e "${red}Hello Stackoverflow${NC}"
这个答案是正确的,除了对颜色的调用不应放在引号内。
echo -e ${red}"Hello Stackoverflow"${NC}
应该做到的。
如果要提高代码的可读性 ,可以先echo
显字符串,然后再使用sed
添加颜色:
echo 'Hello World!' | sed $'s/World/\e[1m&\e[0m/'
这些代码可在我的Ubuntu机器上使用:
echo -e "\x1B[31m foobar \x1B[0m"echo -e "\x1B[32m foobar \x1B[0m"echo -e "\x1B[96m foobar \x1B[0m"echo -e "\x1B[01;96m foobar \x1B[0m"echo -e "\x1B[01;95m foobar \x1B[0m"echo -e "\x1B[01;94m foobar \x1B[0m"echo -e "\x1B[01;93m foobar \x1B[0m"echo -e "\x1B[01;91m foobar \x1B[0m"echo -e "\x1B[01;90m foobar \x1B[0m"echo -e "\x1B[01;89m foobar \x1B[0m"echo -e "\x1B[01;36m foobar \x1B[0m"
这会以不同的颜色打印字母abcd:
echo -e "\x1B[0;93m a \x1B[0m b \x1B[0;92m c \x1B[0;93m d \x1B[0;94m"
对于循环:
for (( i = 0; i < 17; i++ )); do echo "$(tput setaf $i)This is ($i) $(tput sgr0)"; done
这是颜色开关 \\033[
。 查看 。
颜色例如1;32
(浅绿色), 0;34
(蓝色), 1;34
(浅蓝色)等。
我们用彩色开关终止颜色序列\\033[
和0m
, 无 -color代码。 就像以标记语言打开和关闭选项卡一样。
SWITCH="\033[" NORMAL="${SWITCH}0m" YELLOW="${SWITCH}1;33m" echo "${YELLOW}hello, yellow${NORMAL}"
简单的颜色echo
功能解决方案:
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!"
# ResetColor_Off='\033[0m' # Text Reset# Regular ColorsBlack='\033[0;30m' # BlackRed='\033[0;31m' # RedGreen='\033[0;32m' # GreenYellow='\033[0;33m' # YellowBlue='\033[0;34m' # BluePurple='\033[0;35m' # PurpleCyan='\033[0;36m' # CyanWhite='\033[0;37m' # White# BoldBBlack='\033[1;30m' # BlackBRed='\033[1;31m' # RedBGreen='\033[1;32m' # GreenBYellow='\033[1;33m' # YellowBBlue='\033[1;34m' # BlueBPurple='\033[1;35m' # PurpleBCyan='\033[1;36m' # CyanBWhite='\033[1;37m' # White# UnderlineUBlack='\033[4;30m' # BlackURed='\033[4;31m' # RedUGreen='\033[4;32m' # GreenUYellow='\033[4;33m' # YellowUBlue='\033[4;34m' # BlueUPurple='\033[4;35m' # PurpleUCyan='\033[4;36m' # CyanUWhite='\033[4;37m' # White# BackgroundOn_Black='\033[40m' # BlackOn_Red='\033[41m' # RedOn_Green='\033[42m' # GreenOn_Yellow='\033[43m' # YellowOn_Blue='\033[44m' # BlueOn_Purple='\033[45m' # PurpleOn_Cyan='\033[46m' # CyanOn_White='\033[47m' # White# High IntensityIBlack='\033[0;90m' # BlackIRed='\033[0;91m' # RedIGreen='\033[0;92m' # GreenIYellow='\033[0;93m' # YellowIBlue='\033[0;94m' # BlueIPurple='\033[0;95m' # PurpleICyan='\033[0;96m' # CyanIWhite='\033[0;97m' # White# Bold High IntensityBIBlack='\033[1;90m' # BlackBIRed='\033[1;91m' # RedBIGreen='\033[1;92m' # GreenBIYellow='\033[1;93m' # YellowBIBlue='\033[1;94m' # BlueBIPurple='\033[1;95m' # PurpleBICyan='\033[1;96m' # CyanBIWhite='\033[1;97m' # White# High Intensity backgroundsOn_IBlack='\033[0;100m' # BlackOn_IRed='\033[0;101m' # RedOn_IGreen='\033[0;102m' # GreenOn_IYellow='\033[0;103m' # YellowOn_IBlue='\033[0;104m' # BlueOn_IPurple='\033[0;105m' # PurpleOn_ICyan='\033[0;106m' # CyanOn_IWhite='\033[0;107m' # White
| | bash | hex | octal | NOTE ||-------+-------+--------+---------+------------------------------|| start | \e | \x1b | \033 | || start | \E | \x1B | - | x cannot be capital || end | \e[0m | \x1m0m | \033[0m | || end | \e[m | \x1b[m | \033[m | 0 is appended if you omit it || | | | | |
| color | bash | hex | octal | NOTE ||-------------+--------------+----------------+----------------+---------------------------------------|| start green | \e[32m| \x1b[32m | \033[32m | m is NOT optional || reset | \e[0m | \1xb[0m | \033[om | o is optional (do it as best practice || | | | | |
如果您打算在特殊的bash变量中使用这些代码
您应该添加额外的转义字符,以便可以正确解释它们。 如果没有添加额外的转义字符,它就可以工作,但是当您使用Ctrl + r
在历史记录中进行搜索时,您将遇到问题。
您应该在任何开始的ANSI代码之前添加\\[
,在任何结束的ANSI代码之后添加\\]
。
\\033[32mThis is in green\\033[0m
对于PS0 / 1/2/4: \\[\\033[32m\\]This is in green\\[\\033[m\\]
\\[
用于表示一系列不可打印的字符
\\]
用于不可打印字符序列的结尾 提示:要记住,您可以先添加\\[\\]
,然后将ANSI代码放在它们之间:
\\[start-ANSI-code\\]
- \\[end-ANSI-code\\]
在深入研究这些颜色之前,您应该了解以下代码的4种模式:
它修改了非文本颜色的样式。 例如,使颜色变亮或变暗。
0
重置 1;
比正常轻 2;
比正常暗 此模式不受广泛支持。 它完全支持Gnome终端。
此模式用于修改文本的样式,而不是颜色。
3;
斜体 4;
强调 5;
闪烁(慢) 6;
闪烁(快速) 7;
相反 8;
隐藏 9;
划掉 并且几乎得到支持。
例如,KDE-Konsole支持5;
但是Gnome终端不支持,而Gnome支持8;
但是KDE不会。 此模式用于给前景着色。
此模式用于给背景着色。
下表总结了ANSI-color的3/4位版本
|------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| color-mode | octal | hex | bash | description | example (= in octal) | NOTE ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 0 | \033[0m | \x1b[0m | \e[0m | reset any affect | echo -e "\033[0m" | 0m equals to m || 1 | \033[1m | | | light (= bright) | echo -e "\033[1m####\033[m" | - || 2 | \033[2m | | | dark (= fade) | echo -e "\033[2m####\033[m" | - ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| text-mode | ~ | | | ~ | ~ | ~ ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 3 | \033[3m | | | italic | echo -e "\033[3m####\033[m" | || 4 | \033[4m | | | underline | echo -e "\033[4m####\033[m" | || 5 | \033[5m | | | blink (slow) | echo -e "\033[3m####\033[m" | || 6 | \033[6m | | | blink (fast) | ? | not wildly support || 7 | \003[7m | | | reverse | echo -e "\033[7m####\033[m" | it affects the background/foreground || 8 | \033[8m | | | hide | echo -e "\033[8m####\033[m" | it affects the background/foreground || 9 | \033[9m | | | cross | echo -e "\033[9m####\033[m" | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| foreground | ~ | | | ~ | ~ | ~ ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 30 | \033[30m | | | black | echo -e "\033[30m####\033[m" | || 31 | \033[31m | | | red | echo -e "\033[31m####\033[m" | || 32 | \033[32m | | | green | echo -e "\033[32m####\033[m" | || 33 | \033[32m | | | yellow | echo -e "\033[33m####\033[m" | || 34 | \033[32m | | | blue | echo -e "\033[34m####\033[m" | || 35 | \033[32m | | | purple | echo -e "\033[35m####\033[m" | real name: magenta = reddish-purple || 36 | \033[32m | | | cyan | echo -e "\033[36m####\033[m" | || 37 | \033[32m | | | white | echo -e "\033[37m####\033[m" | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 38 | 8/24 | This is for special use of 8-bit or 24-bit ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| background | ~ | | | ~ | ~ | ~ ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 40 | \033[40m | | | black | echo -e "\033[40m####\033[m" | || 41 | \033[41m | | | red | echo -e "\033[41m####\033[m" | || 42 | \033[42m | | | green | echo -e "\033[42m####\033[m" | || 43 | \033[43m | | | yellow | echo -e "\033[43m####\033[m" | || 44 | \033[44m | | | blue | echo -e "\033[44m####\033[m" | || 45 | \033[45m | | | purple | echo -e "\033[45m####\033[m" | real name: magenta = reddish-purple || 46 | \033[46m | | | cyan | echo -e "\033[46m####\033[m" | || 47 | \033[47m | | | white | echo -e "\033[47m####\033[m" | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|| 48 | 8/24 | This is for special use of 8-bit or 24-bit | ||------------+----------+---------+-------+------------------+------------------------------+--------------------------------------|
下表总结了ANSI-color的8位版本
|------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| foreground | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| 0-7 | \033[38;5 | \x1b[38;5 | \e[38;5 | standard. normal | echo -e '\033[38;5;1m####\033[m' | || 8-15 | | | | standard. light | echo -e '\033[38;5;9m####\033[m' | || 16-231 | | | | more resolution | echo -e '\033[38;5;45m####\033[m' | has no specific pattern || 232-255 | | | | | echo -e '\033[38;5;242m####\033[m' | from black to white ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| foreground | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|| 0-7 | | | | standard. normal | echo -e '\033[48;5;1m####\033[m' | || 8-15 | | | | standard. light | echo -e '\033[48;5;9m####\033[m' | || 16-231 | | | | more resolution | echo -e '\033[48;5;45m####\033[m' | || 232-255 | | | | | echo -e '\033[48;5;242m####\033[m' | from black to white ||------------+-----------+-----------+---------+------------------+------------------------------------+-------------------------|
8位快速测试:
for code in {0..255}; do echo -e "\\e[38;05;${code}m $code: Test"; done
下表总结了ANSI-color的24位版本
|------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| foreground | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | R = red | echo -e '\033[38;2;255;0;02m####\033[m' | R=255, G=0, B=0 || 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | G = green | echo -e '\033[38;2;;0;255;02m####\033[m' | R=0, G=255, B=0 || 0-255 | \033[38;2 | \x1b[38;2 | \e[38;2 | B = blue | echo -e '\033[38;2;0;0;2552m####\033[m' | R=0, G=0, B=255 ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| background | octal | hex | bash | description | example | NOTE ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|| 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | R = red | echo -e '\033[48;2;255;0;02m####\033[m' | R=255, G=0, B=0 || 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | G = green | echo -e '\033[48;2;;0;255;02m####\033[m' | R=0, G=255, B=0 || 0-255 | \033[48;2 | \x1b[48;2 | \e[48;2 | B = blue | echo -e '\033[48;2;0;0;2552m####\033[m' | R=0, G=0, B=255 ||------------+-----------+-----------+---------+-------------+------------------------------------------+-----------------|
.gif
前景8位摘要
.gif
背景8位摘要
blinking
一个简单的C
代码,向您展示更多
我开发的用于处理这些颜色的更高级的工具:
是的你可以。 我对 , , , ,
我觉得不行。
3/4位是,如果您使用gcc
编译代码
\\033[
= 2,其他部分1
任何有tty
解释器的地方
xterm
, gnome-terminal
, kde-terminal
, mysql-client-CLI
等。 例如,如果要使用mysql着色输出,可以使用Perl
#!/usr/bin/perl -nprint "\033[1m\033[31m$1\033[36m$2\033[32m$3\033[33m$4\033[m" while /([|+-]+)|([0-9]+)|([a-zA-Z_]+)|([^\w])/g;
将此代码存储在文件名: pcc
(= Perl Colorize Character)中,然后将文件a放入有效的PATH
然后在您喜欢的任何地方使用它。
ls | pcc
df | pcc
在mysql
内部,首先为pager
注册它,然后尝试:
[user2:db2] pager pccPAGER set to 'pcc'[user2:db2] select * from table-name;
它不处理Unicode的。
不,他们可以做很多有趣的事情。 尝试:
echo -e '\033[2K' # clear the screen and do not move the position
要么:
echo -e '\033[2J\033[u' # clear the screen and reset the position
有很多初学者想用system( "clear" )
来清除屏幕,因此您可以使用它代替system(3)
调用
是。 \
使用3/4-bit
很容易,但是使用24-bit
则非常准确和美观。
00000000
和00000000
和00000000
。 每个8位用于特定颜色。 1..8
用于 和9..16
用于 和17..24
为 所以在 #FF0000
表示 这是: 255;0;0
在 #00FF00
表示 这是: 0;255;0
那有意义吗? 您想要什么颜色将其与这三个8位值组合在一起。 参考:
我不记得的一些博客/网页这是我最近整理的一个简单小脚本,它将为任何管道输入着色,而不是使用“厕所”。
File: color.bsh
#!/usr/bin/env bash ## A.M.Danischewski 2015+(c) Free - for (all (uses and ## modifications)) - except you must keep this notice intact. declare INPUT_TXT=""declare ADD_LF="\n" declare -i DONE=0declare -r COLOR_NUMBER="${1:-247}"declare -r ASCII_FG="\\033[38;05;"declare -r COLOR_OUT="${ASCII_FG}${COLOR_NUMBER}m"function show_colors() { ## perhaps will add bg 48 to first loop eventually for fgbg in 38; do for color in {0..256} ; do echo -en "\\033[${fgbg};5;${color}m ${color}\t\\033[0m"; (($((${color}+1))%10==0)) && echo; done; echo; done} if [[ ! $# -eq 1 || ${1} =~ ^-. ]]; then show_colors echo " Usage: ${0##*/}" echo " E.g. echo \"Hello world!\" | figlet | ${0##*/} 54" else while IFS= read -r PIPED_INPUT || { DONE=1; ADD_LF=""; }; do PIPED_INPUT=$(sed 's#\\#\\\\#g' <<< "${PIPED_INPUT}") INPUT_TXT="${INPUT_TXT}${PIPED_INPUT}${ADD_LF}" ((${DONE})) && break; done echo -en "${COLOR_OUT}${INPUT_TXT}\\033[00m"fi
然后将其命名为红色(196):
$> echo "text you want colored red" | color.bsh 196
就像有些东西一样,通过grep将其突出显示为红色(但只能是红色)。 您还可以使用命名管道,使您的字符串更接近行尾:
grep '.*' --color=always <(echo "foobar")
为了扩展 ,我们懒惰的人:
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"
到目前为止,我最喜欢的答案是coloredEcho。
只是要发布另一个选项,您可以查看这个小工具xcol
您就像grep一样使用它,并且它将为每个参数为stdin着色不同的颜色,例如
sudo netstat -putan | xcol httpd sshd dnsmasq pulseaudio conky tor Telegram firefox "[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+\.[[:digit:]]+" ":[[:digit:]]+" "tcp." "udp." LISTEN ESTABLISHED TIME_WAIT
请注意,它接受sed将接受的任何正则表达式。
该工具使用以下定义
#normal=$(tput sgr0) # normal textnormal=$'\e[0m' # (works better sometimes)bold=$(tput bold) # make colors bold/brightred="$bold$(tput setaf 1)" # bright red textgreen=$(tput setaf 2) # dim green textfawn=$(tput setaf 3); beige="$fawn" # dark yellow textyellow="$bold$fawn" # bright yellow textdarkblue=$(tput setaf 4) # dim blue textblue="$bold$darkblue" # bright blue textpurple=$(tput setaf 5); magenta="$purple" # magenta textpink="$bold$purple" # bright magenta textdarkcyan=$(tput setaf 6) # dim cyan textcyan="$bold$darkcyan" # bright cyan textgray=$(tput setaf 7) # dim white textdarkgray="$bold"$(tput setaf 0) # bold black = dark gray textwhite="$bold$gray" # bright white text
我像这样在脚本中使用这些变量
echo "${red}hello ${yellow}this is ${green}coloured${normal}"
这就是我用来查看所有组合并确定哪个显示很酷的内容:
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" donedone
我写了来实现这一目标。
你可以做
pip install swag
现在,您可以通过以下方式将所有转义命令以txt文件的形式安装到给定的目的地:
swag install -d
甚至更容易通过:
swag install
将颜色安装到~/.colors
。
您可以像这样使用它们:
echo $(cat ~/.colors/blue.txt) This will be blue
或这样,我发现实际上更有趣:
swag print -c red -t underline "I will turn red and be underlined"
检查出 !
使用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"
感谢@ k-five的答案
declare -A colors#curl www.bunlongheng.com/code/colors.png# Resetcolors[Color_Off]='\033[0m' # Text Reset# Regular Colorscolors[Black]='\033[0;30m' # Blackcolors[Red]='\033[0;31m' # Redcolors[Green]='\033[0;32m' # Greencolors[Yellow]='\033[0;33m' # Yellowcolors[Blue]='\033[0;34m' # Bluecolors[Purple]='\033[0;35m' # Purplecolors[Cyan]='\033[0;36m' # Cyancolors[White]='\033[0;37m' # White# Boldcolors[BBlack]='\033[1;30m' # Blackcolors[BRed]='\033[1;31m' # Redcolors[BGreen]='\033[1;32m' # Greencolors[BYellow]='\033[1;33m' # Yellowcolors[BBlue]='\033[1;34m' # Bluecolors[BPurple]='\033[1;35m' # Purplecolors[BCyan]='\033[1;36m' # Cyancolors[BWhite]='\033[1;37m' # White# Underlinecolors[UBlack]='\033[4;30m' # Blackcolors[URed]='\033[4;31m' # Redcolors[UGreen]='\033[4;32m' # Greencolors[UYellow]='\033[4;33m' # Yellowcolors[UBlue]='\033[4;34m' # Bluecolors[UPurple]='\033[4;35m' # Purplecolors[UCyan]='\033[4;36m' # Cyancolors[UWhite]='\033[4;37m' # White# Backgroundcolors[On_Black]='\033[40m' # Blackcolors[On_Red]='\033[41m' # Redcolors[On_Green]='\033[42m' # Greencolors[On_Yellow]='\033[43m' # Yellowcolors[On_Blue]='\033[44m' # Bluecolors[On_Purple]='\033[45m' # Purplecolors[On_Cyan]='\033[46m' # Cyancolors[On_White]='\033[47m' # White# High Intensitycolors[IBlack]='\033[0;90m' # Blackcolors[IRed]='\033[0;91m' # Redcolors[IGreen]='\033[0;92m' # Greencolors[IYellow]='\033[0;93m' # Yellowcolors[IBlue]='\033[0;94m' # Bluecolors[IPurple]='\033[0;95m' # Purplecolors[ICyan]='\033[0;96m' # Cyancolors[IWhite]='\033[0;97m' # White# Bold High Intensitycolors[BIBlack]='\033[1;90m' # Blackcolors[BIRed]='\033[1;91m' # Redcolors[BIGreen]='\033[1;92m' # Greencolors[BIYellow]='\033[1;93m' # Yellowcolors[BIBlue]='\033[1;94m' # Bluecolors[BIPurple]='\033[1;95m' # Purplecolors[BICyan]='\033[1;96m' # Cyancolors[BIWhite]='\033[1;97m' # White# High Intensity backgroundscolors[On_IBlack]='\033[0;100m' # Blackcolors[On_IRed]='\033[0;101m' # Redcolors[On_IGreen]='\033[0;102m' # Greencolors[On_IYellow]='\033[0;103m' # Yellowcolors[On_IBlue]='\033[0;104m' # Bluecolors[On_IPurple]='\033[0;105m' # Purplecolors[On_ICyan]='\033[0;106m' # Cyancolors[On_IWhite]='\033[0;107m' # Whitecolor=${colors[$input_color]}white=${colors[White]}# echo $whitefor i in "${!colors[@]}"do echo -e "$i = ${colors[$i]}I love you$white"done
希望这张帮助您为bash选择颜色:D
这个问题已经被一遍又一遍地回答:-),但是为什么不呢。
第一次使用tput
在现代环境中比通过echo -E
手动注入ASCII代码更容易携带
这是一个快速的bash函数:
say() { echo "$@" | sed \ -e "s/\(\(@\(red\|green\|yellow\|blue\|magenta\|cyan\|white\|reset\|b\|u\)\)\+\)[[]\{2\}\(.*\)[]]\{2\}/\1\4@reset/g" \ -e "s/@red/$(tput setaf 1)/g" \ -e "s/@green/$(tput setaf 2)/g" \ -e "s/@yellow/$(tput setaf 3)/g" \ -e "s/@blue/$(tput setaf 4)/g" \ -e "s/@magenta/$(tput setaf 5)/g" \ -e "s/@cyan/$(tput setaf 6)/g" \ -e "s/@white/$(tput setaf 7)/g" \ -e "s/@reset/$(tput sgr0)/g" \ -e "s/@b/$(tput bold)/g" \ -e "s/@u/$(tput sgr 0 1)/g" }
现在您可以使用:
say @b@green[[Success]]
要得到:
tput
可移植性的注意事项 1986年9月首次上载tput(1)
源代码
tput(1)
在1990年代的X / Open curses语义中可用(1997标准具有以下提及的语义)。
因此,它是( 相当 )普遍。
我们可以为文本和背景使用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("input",update) back.addEventListener("input",update) function update(){ let a = text.value.substr(1).match(/.{1,2}/g) let 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;font-size:larger}
TEXT COLOR: BACK COLOR:
24位:具有16至24位颜色的“真彩色”图形卡变得很普遍,Xterm,KDE的Konsole以及所有基于libvte的终端(包括GNOME终端)都支持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
参见
绝对应该在原始ANSI控制序列上使用tput。
因为存在大量不同的终端控制语言,所以系统通常具有中间通信层。 在数据库中查找当前检测到的终端类型的实际代码,然后向API发出标准请求,或者(从外壳程序)向命令发出标准请求。
这些命令之一是
tput
。tput
接受一组称为功能名称和任何参数(如果适用)的首字母缩写词,然后在terminfo数据库中为检测到的终端查找正确的转义序列,并输出正确的代码(终端希望能理解)。
来自
就是说,我编写了一个名为的小型帮助程序库,该库在tput之上添加了另一层,使其更易于使用(imho):
示例: tint "white(Cyan(T)Magenta(I)Yellow(N)Black(T)) is bold(really) easy to use."
将给出以下结果:
这是最简单易读的解决方案。 使用bashj( ),您只需选择以下行之一:
#!/usr/bin/bashW="Hello world!"echo $WR=130G=60B=190echo 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 $Yecho u.bold($Y)
如果终端应用程序中有颜色支持,则可以使用256x256x256
种颜色。
我刚刚合并了所有解决方案中的好方法,最后得出:
cecho(){ RED="\033[0;31m" GREEN="\033[0;32m" YELLOW="\033[1;33m" # ... ADD MORE COLORS NC="\033[0m" # No Color printf "${!1}${2} ${NC}\n"}
您可以将其称为:
cecho "RED" "Helloworld"
参考:
echo_red(){ echo -e "\e[1;31m$1\e[0m"}echo_green(){ echo -e "\e[1;32m$1\e[0m"}echo_yellow(){ echo -e "\e[1;33m$1\e[0m"}echo_blue(){ echo -e "\e[1;34m$1\e[0m"}
这是一个在MacOS终端上工作的实现,用于为PS1设置颜色以及不设置颜色。
有两种实现方式,一种是依靠echo,另一种是依靠printf来动态调用方法而又不会松懈。
这只是一个开始,但功能强大,不会闪烁终端。 现在支持git branch,但是最终可以扩展为做很多事情。
可以在这里找到:
应该可以通过复制和粘贴来工作。 没有依赖关系。
要显示不同颜色的消息输出,可以使:
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
如果您使用的是zsh
或bash
black() { echo -e "\e[30m${1}\e[0m"}red() { echo -e "\e[31m${1}\e[0m"}green() { echo -e "\e[32m${1}\e[0m"}yellow() { echo -e "\e[33m${1}\e[0m"}blue() { echo -e "\e[34m${1}\e[0m"}magenta() { echo -e "\e[35m${1}\e[0m"}cyan() { echo -e "\e[36m${1}\e[0m"}gray() { echo -e "\e[90m${1}\e[0m"}black 'BLACK'red 'RED'green 'GREEN'yellow 'YELLOW'blue 'BLUE'magenta 'MAGENTA'cyan 'CYAN'gray 'GRAY'
从线程混合其他解决方案后,这就是我如何在npm scripts
输出(gitbash CLI)中获取颜色的方法:
{ "deploy": "echo \u001b[1;32m && ng build && echo \u001b[1;0mdeploy {\u001b[1;33mcopy\u001b[1;0m: \u001b[1;32m0% && cp -r -f dist/packaged/* \\\\SERVER-01\\dist\\ && echo \u001b[1;0mdeploy {\u001b[1;33mcopy\u001b[1;0m} \u001b[1;34m\u001b[1;1m100% DEPLOYED"}
您可以在bash脚本中定义颜色,如下所示:
red=$'\e[1;31m'grn=$'\e[1;32m'yel=$'\e[1;33m'blu=$'\e[1;34m'mag=$'\e[1;35m'cyn=$'\e[1;36m'end=$'\e[0m'
然后使用它们以您需要的颜色进行打印:
printf "%s\n" "Text in ${red}red${end}, white and ${blu}blue${end}."
echo -e "\033[31m Hello World"
[31m
控制文本颜色:
30
- 37
套前景色 40
- 47
套背景颜色 更完整的颜色代码列表。
优良作法是在字符串末尾将文本颜色重置回\\033[0m
。
使用具有setaf
功能和参数1
tput
。
echo "$(tput setaf 1)Hello, world$(tput sgr0)"
您可以使用以下 :
Black 0;30 Dark Gray 1;30Red 0;31 Light Red 1;31Green 0;32 Light Green 1;32Brown/Orange 0;33 Yellow 1;33Blue 0;34 Light Blue 1;34Purple 0;35 Light Purple 1;35Cyan 0;36 Light Cyan 1;36Light Gray 0;37 White 1;37
然后在脚本中像这样使用它们:
# .---------- constant part!# vvvv vvvv-- the code from aboveRED='\033[0;31m'NC='\033[0m' # No Colorprintf "I ${RED}love${NC} Stack Overflow\n"
用红色印出love
。
根据@ james-lim的注释, 如果使用的是echo
命令,请确保使用-e标志以允许反斜杠转义 。
# Continued from above exampleecho -e "I ${RED}love${NC} Stack Overflow"
(除非要添加其他空行,否则在使用echo时请勿添加"\\n"
)
转载地址:http://keogj.baihongyu.com/