Bash Shell怎么打印各种颜色
刚刚在微博上看到这个,一直没弄明白Bash的颜色是怎么一个规则?
怎么控制颜色,打印出有规则的颜色?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
刚刚在微博上看到这个,一直没弄明白Bash的颜色是怎么一个规则?
怎么控制颜色,打印出有规则的颜色?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
Bash Shell定义文字颜色有三个参数:Style,Frontground和Background,每个参数有7个值,意义如下:
其中,+30表示前景色,+40表示背景色
这里提供一段代码可以打印颜色表:
补充一个xterm颜色表(来自Wikipedia)
之前写过一篇博文,可以参考一下:http://www.wuzesheng.com/?p=2177
wonderful!确实漂亮,以前没用过,不过我想这个可能是你的答案
http://webhome.csc.uvic.ca/~sae/seng2...
用的是Escape Sequence:
http://en.wikipedia.org/wiki/Escape_s...
至于具体怎么打印颜色,可参考
https://wiki.archlinux.org/index.php/...
http://www.linuxselfhelp.com/howtos/B...
here: ansi-color
如果仅仅是打印日志,我之前写过一个日志库,里面有打印颜色的用法:https://github.com/dangoakachan/logdo...
对echo做过一个简单包装。。。
第一个GitHub上的作业。。。
只支持ascii颜色。。。
cecho,直接把readme拷贝过来了。
cecho
Colorful echo for unix-like shell ( ascii color only )
Overview
A light, self-adapting wrapper of
echo
, with color flags of foreground, background and action ( highlight, underline, blink ... ) . With it, you can easily colorize your output of command and shell script.Tested in zsh and bash.
Screenshot
Active
Inactive
Install
Before use
cecho
, simply source it in your .zshrc, or any shell script wanted to colorize like this:Usage
Support multi-string in one command:
Support setting foreground, background and action
All flags have two formats, short for convenience, long for readability.
The short formats is easily memorized, which are nearly just the heads the long formats.
Foreground color flag
Background color flag
Action flag
Escaped character
Functional
Example
set fg to red:
or
or
blank & tab & newline
set fg to red, bg to yellow, action to highlight and underline:
set [ red, yellow, highlight & underline ] for "hello", [ cyan, red, reverse ] for "world", [ green, black, blink ] for "!" :
More examples in help file triggered with this command
cecho -h
That colorful help page is generated by
cecho
.Useful Variables
CECHO_IS_IMPORTED
Once you have sourced
cecho
,CECHO_IS_IMPORTED=1
will be defined.That meams you can check this var
CECHO_IS_IMPORTED
to confirm if in a no_cecho env or not:Exp: Sence 2
CECHO_IS_INACTIVE
When you use
cecho
but want it inactive temply, setCECHO_IS_INACTIVE=1
before callingcecho
.Note: color and action can be disable by
CECHO_IS_INACTIVE
, but we keepreturn
,blank
andtab
active, as you wish.Sence
1 - I want my script outputs to file/pipe without color_ctrl chars
Needn't do any change,
cecho
can self-adapting this: colorful for stdout, non-colorful for file/pipe.2 - My script maybe runs in both "no-cecho env" and "cecho env"
Unfortunately, you have to write your script like this:
Or shortly:
3 - My script uses
cecho
but I want it disabled templyAdvanced
-d
( same as-done
) will turn off all settings ( fg, bg and action ), Thus, in single command, you have to set flags again to make it work well once there are other strings after-d
.cecho "!"
. It's due toecho
... usececho '!'
orcecho !
instead.-d
at the begin/end of commands,cecho
do it automatically.What's more
Another C version project is coprintf.
Thanks
Thanks to https://github.com/jonhiggs/cecho, when I completed
cecho
script and try uploading it to github I found this -- with same name and almost same functions ... and then, I got the way to build table in README.md from there.