linux shell 输出红色,Bash Shell怎么打印各种颜色

对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

051460af00c7d6c87707adfc3cc1ed82.png

Inactive

adbb5c1d6b596c6e4578233808dc7ffc.png

Install

Before use cecho, simply source it in your .zshrc, or any shell script wanted to colorize like this:

source /path/to/cecho.sh

Usage

Support multi-string in one command:

cecho -color1 message1 [ -color2 message2 ... ] [ -done ]

Support setting foreground, background and action

cecho -fg_color -bg_color -action message [...] [ -done ]

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

short

long

-r

-red

-g

-green

-y

-yellow

-b

-blue

-p

-purple

-c

-cyan

-w

-white

-gr

-gray

-bk

-black

Background color flag

short

long

-br

-b_red

-bg

-b_green

-by

-b_yellow

-bb

-b_blue

-bp

-b_purple

-bc

-b_cyan

-bw

-b_white

-bgr

-b_gray

-bbk

-b_black

Action flag

short

long

-d

-done

-hl

-highlight

-ul

-underline

-re

-reverse

-bl

-blink

-iv

-invisible

Escaped character

short

long

-B

-blank

-t

-tab

-n

-newline

Functional

short

long

-h

-help,--help

Example

set fg to red:

cecho -r "hello world ! "

or

cecho -red "hello world ! "

or

cecho -r hello -B world -B !

blank & tab & newline

cecho -r -t hello -B world -n !

set fg to red, bg to yellow, action to highlight and underline:

cecho -r -by -hl "hello world ! "

set [ red, yellow, highlight & underline ] for "hello", [ cyan, red, reverse ] for "world", [ green, black, blink ] for "!" :

cecho -r -by -hl -ul hello -d -B -c -br -re world -d -B -g -bl !

More examples in help file triggered with this command

cecho -h

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:

CECHO_IS_INACTIVE

When you use cecho but want it inactive temply, set CECHO_IS_INACTIVE=1 before calling cecho.

Note: color and action can be disable by CECHO_IS_INACTIVE, but we keep return, blank and tab 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:

if [ -z "$CECHO_IS_IMPORTED" ]

then

echo hello world

else

cecho -r -bg -hl hello -B world

fi

Or shortly:

[ -z "$CECHO_IS_IMPORTED" ] && echo "hello world" || cecho -r -bg -hl hello -B world

3 - My script uses cecho but I want it disabled temply

CECHO_IS_INACTIVE=1

cecho -r -bg -hl hello world #

CECHO_IS_INACTIVE=

cecho -r -bg -hl hello world #

Advanced

Action flag -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.

Some special characters ( with centain format ) don't work well, such as cecho "!". It's due to echo... use cecho '!' or cecho ! instead.

It's not necessary use -d at the begin/end of commands, cecho do it automatically.

Actions can overlaied.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值