Linux 彩色 shell 脚本输出库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16843382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:05:48  来源:igfitidea点击:

Colored shell script output library

linuxmacosshellcolors

提问by Bruno Penteado

I am looking for a definitive way to build shell scripts that generates colored output.

我正在寻找一种确定的方法来构建生成彩色输出的 shell 脚本。

Unfortunately I am having a hard time finding an appropriate lib or good technique for doing this. I found a lot of helpful but simple examples like this. Also the most comprehensive guide that I found until now is this one.

不幸的是,我很难找到合适的库或好的技术来做到这一点。我发现了很多像这样有用但简单的例子。到目前为止,我发现的最全面的指南也是这个指南。

Before I start writing my own library, I want to check if anyone already wrote it

在我开始编写自己的库之前,我想检查一下是否有人已经编写了它

If your solution does not fit into the observations below thats not a problem. I would like also to read it so it can help me out if decide to write my own solution

如果您的解决方案不符合以下观察结果,那不是问题。我也想阅读它,以便在决定编写自己的解决方案时可以帮助我

My main concerns/observations:

我的主要关注点/观察结果:

  • Needs to be safe. Want to avoid garbage output as not all terminals or pagers/editors (like less, more, vim, and so on) support colored output or more styled output (bold, blinked, italic, etc)
  • Needs to be easy and readable. Using ANSI escape codesdirectly is horrible: echo -e '\033[32mthis is ugly and \033[1;32mvery green\033[0m'
  • Needs to give me access to the whole color palette and stylesfor foreground and background text. Most of the examples I found uses only the basic colors for foreground text only.
  • Its preferable to use only simple commandslike bash or simpler shells built in commands and/or common commands that can be found on most operating systems. For instance I can use colorizebut I would need ruby (that's somewhat ok) and the colorize gem installed (not ok)
  • Tputseems to be a good optionas it can manipulate the shell cursor quite well, but it is somewhat simpler/less flexible
  • 需要安全。想要避免垃圾输出,因为并非所有终端或寻呼机/编辑器(如 less、more、vim 等)都支持彩色输出或更多样式的输出(粗体、闪烁、斜体等)
  • 需要简单易读。直接使用ANSI 转义码是可怕的:echo -e '\033[32mthis is ugly and \033[1;32mvery green\033[0m'
  • 需要让我访问整个调色板和前景和背景文本的样式。我发现的大多数示例仅将基本颜色用于前景文本。
  • 最好只使用简单的命令,如 bash 或内置命令和/或大多数操作系统上可以找到的常见命令的更简单的 shell。例如,我可以使用colorize但我需要 ruby​​(这有点好)和安装了 colorize gem(不好)
  • Tput似乎是一个不错的选择,因为它可以很好地操作 shell 光标,但它有点简单/不太灵活

Edit

编辑

After some research on terminal control and output formatting, I am writing this gistthat tries to accomplish this. So far it is doing quite well

在对终端控制和输出格式进行了一些研究之后,我正在编写这个试图实现这一点的要点。到目前为止它做得很好

采纳答案by demure

Here is an modified snippet from my dotfilesthat should do what you want

这是我的dotfiles中的一个修改后的片段,它应该可以满足您的需求

RCol='\e[0m'    # Text Reset

# Regular           Bold                Underline           High Intensity      BoldHigh Intens     Background          High Intensity Backgrounds
Bla='\e[0;30m';     BBla='\e[1;30m';    UBla='\e[4;30m';    IBla='\e[0;90m';    BIBla='\e[1;90m';   On_Bla='\e[40m';    On_IBla='\e[0;100m';
Red='\e[0;31m';     BRed='\e[1;31m';    URed='\e[4;31m';    IRed='\e[0;91m';    BIRed='\e[1;91m';   On_Red='\e[41m';    On_IRed='\e[0;101m';
Gre='\e[0;32m';     BGre='\e[1;32m';    UGre='\e[4;32m';    IGre='\e[0;92m';    BIGre='\e[1;92m';   On_Gre='\e[42m';    On_IGre='\e[0;102m';
Yel='\e[0;33m';     BYel='\e[1;33m';    UYel='\e[4;33m';    IYel='\e[0;93m';    BIYel='\e[1;93m';   On_Yel='\e[43m';    On_IYel='\e[0;103m';
Blu='\e[0;34m';     BBlu='\e[1;34m';    UBlu='\e[4;34m';    IBlu='\e[0;94m';    BIBlu='\e[1;94m';   On_Blu='\e[44m';    On_IBlu='\e[0;104m';
Pur='\e[0;35m';     BPur='\e[1;35m';    UPur='\e[4;35m';    IPur='\e[0;95m';    BIPur='\e[1;95m';   On_Pur='\e[45m';    On_IPur='\e[0;105m';
Cya='\e[0;36m';     BCya='\e[1;36m';    UCya='\e[4;36m';    ICya='\e[0;96m';    BICya='\e[1;96m';   On_Cya='\e[46m';    On_ICya='\e[0;106m';
Whi='\e[0;37m';     BWhi='\e[1;37m';    UWhi='\e[4;37m';    IWhi='\e[0;97m';    BIWhi='\e[1;97m';   On_Whi='\e[47m';    On_IWhi='\e[0;107m';

Then you can just echo -e "${Blu}blue ${Red}red ${RCol}etc...."

那么你就可以 echo -e "${Blu}blue ${Red}red ${RCol}etc...."

回答by Ranjithkumar T

echo -e "\033[33;31m Color Text" - red

echo -e "\033[33;31m 彩色文本" - 红色

echo -e "\033[33;32m Color Text" - green

echo -e "\033[33;32m 彩色文本" - 绿色

echo -e "\033[33;33m Color Text" - yellow

echo -e "\033[33;33m 彩色文本" - 黄色

echo -e "\033[33;34m Color Text" - blue

echo -e "\033[33;34m 彩色文本" - 蓝色

echo -e "\033[33;35m Color Text" - Magenta

echo -e "\033[33;35m 彩色文本" - 洋红色

echo -e "\033[33;30m Color Text" - Gray

echo -e "\033[33;30m 彩色文本" - 灰色

echo -e "\033[33;36m Color Text" - Cyan

echo -e "\033[33;36m 彩色文本" - 青色

http://techietent.blogspot.in/2013/03/how-to-echo-colored-text-in-linux-shell.html

http://techietent.blogspot.in/2013/03/how-to-echo-colored-text-in-linux-shell.html

回答by MGP

Shameless plug... check Rainbow.sh

无耻的插头......检查Rainbow.sh

Usage

用法

Just import rainbow.sh and start using the available functions in your scripts.

只需导入rainbow.sh 并开始使用脚本中的可用函数。

source rainbow.sh 

vargreen=$(echogreen "Grass is green")
varred=$(echored "Roses are red")

echo "$vargreen ..Crickets are noisy.. $varred"

enter image description here

enter image description here

回答by chepner

tputcan handle more than is indicated on the page you link to. All tputdoes is output the characters you would include in your echostatement, based on what appears in the current terminal's termcap/terminfo database. Some examples:

tput可以处理比您链接到的页面上指示的更多。所有tput所做的就是输出的字符,你会在您的echo说法,依据是什么出现在当前终端的的termcap / terminfo数据库。一些例子:

$ tput setaf 5 | hexdump -C
00000000  1b 5b 33 35 6d                                    |.[35m|
$ tput setaf 17 | hexdump -C
00000000  1b 5b 33 38 3b 35 3b 31  37 6d                    |.[38;5;17m|
$ tput reset | hexdump -C
00000000  1b 63 1b 5b 3f 31 30 30  30 6c 1b 5b 3f 32 35 68  |.c.[?1000l.[?25h|

You would use it the same way you use the variable defined in your gist; in fact, you could use it to createyour gist, in a portable fashion:

您可以像使用 gist 中定义的变量一样使用它;事实上,您可以使用它以便携的方式创建您的要点:

black=$(tput setaf 0)

回答by Ian128K

I took demure's list as inspiration and did a little DRYing out of it. (And changed \eto the hexadecimal \x1B, since the former isn't supported in OS X's Terminal.app since Snow Leopard.) Here's what I came up with:

我以娴静的清单为灵感,并对其进行了一些干燥。(并更改\e为十六进制\x1B,因为自 Snow Leopard 以来 OS X 的 Terminal.app 不支持前者。)这是我想出的:

## Colours and font styles
## Syntax: echo -e "${FOREGROUND_COLOUR}${BACKGROUND_COLOUR}${STYLE}Hello world!${RESET_ALL}"

# Escape sequence and resets
ESC_SEQ="\x1b["
RESET_ALL="${ESC_SEQ}0m"
RESET_BOLD="${ESC_SEQ}21m"
RESET_UL="${ESC_SEQ}24m"

# Foreground colours
FG_BLACK="${ESC_SEQ}30;"
FG_RED="${ESC_SEQ}31;"
FG_GREEN="${ESC_SEQ}32;"
FG_YELLOW="${ESC_SEQ}33;"
FG_BLUE="${ESC_SEQ}34;"
FG_MAGENTA="${ESC_SEQ}35;"
FG_CYAN="${ESC_SEQ}36;"
FG_WHITE="${ESC_SEQ}37;"
FG_BR_BLACK="${ESC_SEQ}90;"
FG_BR_RED="${ESC_SEQ}91;"
FG_BR_GREEN="${ESC_SEQ}92;"
FG_BR_YELLOW="${ESC_SEQ}93;"
FG_BR_BLUE="${ESC_SEQ}94;"
FG_BR_MAGENTA="${ESC_SEQ}95;"
FG_BR_CYAN="${ESC_SEQ}96;"
FG_BR_WHITE="${ESC_SEQ}97;"

# Background colours (optional)
BG_BLACK="40;"
BG_RED="41;"
BG_GREEN="42;"
BG_YELLOW="43;"
BG_BLUE="44;"
BG_MAGENTA="45;"
BG_CYAN="46;"
BG_WHITE="47;"

# Font styles
FS_REG="0m"
FS_BOLD="1m"
FS_UL="4m"

The BR_colours are the "bright" or "high-intensity" colours. Done this way, you can even mix them with other font styles. (e.g. underlined bright white)

BR_颜色是“亮”或“高强度”的颜色。通过这种方式,您甚至可以将它们与其他字体样式混合使用。(例如带下划线的亮白色)

If you want to bookmark this, I made a gist for it: https://gist.github.com/ian128K/39a490e5aa8d3bb77a8b

如果你想为此添加书签,我为它做了一个要点:https: //gist.github.com/ian128K/39a490e5aa8d3bb77a8b

回答by nachoparker

I personally use these in my xcol tool that I developed using Andreas Schamanek code as a reference.

我个人在我使用 Andreas Schamanek 代码开发的 xcol 工具中使用这些作为参考。

#normal=$(tput sgr0)                      # normal text
normal=$'\e[0m'                           # (works better sometimes)
bold=$(tput bold)                         # make colors bold/bright
red="$bold$(tput setaf 1)"                # bright red text
green=$(tput setaf 2)                     # dim green text
fawn=$(tput setaf 3); beige="$fawn"       # dark yellow text
yellow="$bold$fawn"                       # bright yellow text
darkblue=$(tput setaf 4)                  # dim blue text
blue="$bold$darkblue"                     # bright blue text
purple=$(tput setaf 5); magenta="$purple" # magenta text
pink="$bold$purple"                       # bright magenta text
darkcyan=$(tput setaf 6)                  # dim cyan text
cyan="$bold$darkcyan"                     # bright cyan text
gray=$(tput setaf 7)                      # dim white text
darkgray="$bold"$(tput setaf 0)           # bold black = dark gray text
white="$bold$gray"                        # bright white text

I use these variables in my scripts like so

我像这样在我的脚本中使用这些变量

echo "${red}hello ${yellow}this is ${green}coloured${normal}"

Checkout my xcol tool for ideas and examples

查看我的 xcol 工具以获取想法和示例

https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/

https://ownyourbits.com/2017/01/23/colorize-your-stdout-with-xcol/

xcol example

xcol example