彩色 Linux 命令输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20909942/
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-07 01:46:05  来源:igfitidea点击:

Color Linux command output

linuxbash

提问by user3158243

For example, I'd like to color the output of the locatecommand so it's easily distinguished from the other terminal text.

例如,我想为locate命令的输出着色,以便很容易将其与其他终端文本区分开来。

It should work something like this:

它应该像这样工作:

locate -bir pdf | some_command_to_color_the_result

Coloring shouldn't be limited for the locatecommand only: I need a generic solution that colors text using pipelines, for example feeding it the output of grepor cat.

着色不应仅限于locate命令:我需要一个通用解决方案,使用管道为文本着色,例如将grep或的输出提供给它cat

If there's no built-in Linux command, any hints on how to create one are appreciated.

如果没有内置的 Linux 命令,任何有关如何创建命令的提示都值得赞赏。

回答by Christian Fritz

You can use escape sequences to change the font color of any output to the bash shell. Here are some of the color codes you'll need:

您可以使用转义序列将任何输出的字体颜色更改为 bash shell。以下是您需要的一些颜色代码:

BLACK="3[30m"
RED="3[31m"
GREEN="3[32m"
YELLOW="3[33m"
BLUE="3[34m"
PINK="3[35m"
CYAN="3[36m"
WHITE="3[37m"
NORMAL="3[0;39m"

Once these are defined, you can use them in normal echo commands. For instance:

一旦定义了这些,您就可以在普通的 echo 命令中使用它们。例如:

echo -e $GREEN this text is green $NORMAL and this is normal

Note that the -e is not always necessary, but on some OSs (incl. osx) is required for to enable escape sequences.

请注意,-e 并非总是必需的,但在某些操作系统(包括 osx)上需要启用转义序列。

Given these definitions you can build scripts and pipes to color the output from other commands. Here is a complete example I use to color the output from svn up:

根据这些定义,您可以构建脚本和管道来为其他命令的输出着色。这是我用来为以下输出着色的完整示例svn up

#!/bin/bash

BLACK="3[30m"
RED="3[31m"
GREEN="3[32m"
YELLOW="3[33m"
BLUE="3[34m"
PINK="3[35m"
CYAN="3[36m"
WHITE="3[37m"
NORMAL="3[0;39m"

TMPFILE=.cvsup.tmp

svn up > $TMPFILE
svn status >> $TMPFILE
printf $YELLOW
grep -e ^"\? " -e ^"I " $TMPFILE
printf $GREEN
grep -e ^"R " -e ^"U " -e ^"G " $TMPFILE
printf $BLUE
grep -e ^"M " -e ^"E " $TMPFILE
printf $RED
grep -e ^"C " -e ^"! " -e ^"X " -e ^"~ " $TMPFILE
printf $PINK
grep ^"R " $TMPFILE
printf $PINK
grep ^"D " $TMPFILE
printf $CYAN
grep ^"A " $TMPFILE

printf $NORMAL
rm $TMPFILE

You can also look at tput.

你也可以看看tput

回答by Olivier Dulac

norm="$(printf '3[0m')" #returns to "normal"
bold="$(printf '3[0;1m')" #set bold
red="$(printf '3[0;31m')" #set red
boldred="$(printf '3[0;1;31m')" #set bold, and set red.

somecommand | sed -e "s/someregexp/${boldred}&${norm}/g"  # will color any occurence of someregexp in Bold red

printf "%s" "$red" ; locate something ; printf "%s" "$norm"  # will color output of locate something in red
   #I (ab)use printf "%s" "something", as it's more portable than echo,and easy to modify

There are many other ways (create a function/script that can colorize a regexp, for example, and then : somecommand | colorize -c green 'foo.*bar' 'other')

还有很多其他的方法(创建一个函数/脚本,可以上色一个正则表达式,例如,然后: somecommand | colorize -c green 'foo.*bar' 'other'

回答by user3158243

The following answered my question:

以下回答了我的问题:

1- I create an alias in my .bashrc

1- 我在我的 .bashrc 中创建了一个别名

alias color='grep --color .'

2- Then whenever I want to color the pipeline text output I use color alias like:

2-然后每当我想为管道文本输出着色时,我都会使用颜色别名,例如:

locate -bir pdf | color

This will color the output to red

这会将输出着色为红色

回答by V H

As suggested by Jonathan Leffler, comment posted as an anwser:

正如乔纳森·莱夫勒 (Jonathan Leffler) 所建议的,作为 anwser 发布的评论:

grep --colorwill provide colour

grep --color将提供颜色

回答by bijancn

There is a far better way to achieve customizable coloring:

有一种更好的方法来实现可自定义的着色:

colorit

颜色

You can use it as shown in other answers via some_command | coloritbut it is nicely configurable over the .coloritrc. In mine I have stuff like

您可以按照其他答案中所示的方式使用它,some_command | colorit但它可以通过.coloritrc. 在我的我有这样的东西

dnl  Define some useful color variables
define(`red', `1')
define(`green', `2')
define(`magenta', `5')
dnl
dnl  Mark macro arguments: regexp foreground-color [background-color]
dnl
define(`mark', ``mark ""'' `ifelse(`$#', `3', ``"3[3;4m"'',
``"3[3m"'')' `"3[m"')
dnl
divert
mark(`warning', magenta)
mark(`Warning', magenta)
mark(`Traceback', magenta)
mark(`Error', red)
mark(`FAIL', red)
mark(`ERROR', red)
mark(`XFAIL', green)
mark(`ok', green)
mark(`OK', green)
mark(`PASS', green)

and use it all the time for coloring compiler output and similar stuff. See my .coloritrcfor more.

并一直使用它来为编译器输出和类似的东西着色。请参阅我的.coloritrc了解更多信息。

回答by Fruit

I prefer use highlight utility:

我更喜欢使用高亮实用程序:

highlight -O xterm256 -S sh

-S shhere means treats the input as shell script syntax.

-S sh这里的意思是将输入视为 shell 脚本语法。

More info: http://www.andre-simon.de/

更多信息:http: //www.andre-simon.de/

I set it as an alias through ~/.bashrc: enter image description here

我通过 ~/.bashrc 将它设置为别名: 在此处输入图片说明

enter image description here

在此处输入图片说明

回答by Bush

I think the hl command available on git hub might help you :
have a look at http://www.flashnux.com/notes/page_000022_US.html

我认为 git hub 上可用的 hl 命令可能会帮助你:
看看http://www.flashnux.com/notes/page_000022_US.html

回答by Bush

You should have a look at the hlcommand available on git hub:

您应该查看hlgit hub 上可用的命令:

git clone http://github.com/mbornet-hl/hl

and on :

并在:

http://www.flashnux.com/notes/page_000022_US.html

http://www.flashnux.com/notes/page_000022_US.html

hlis a Linux command written in C, especially designed to color a text file or the output of a command. You can use up to 42 colors simultaneously, and use a configuration file to simplify command lines. You can colorize the output of every command that can be piped to another one. And if you know what regular expressions are, it will be very easy for you to use. You can use the man pageto understand how to use it.

hl是一个用 C 编写的 Linux 命令,专门设计用于为文本文件或命令的输出着色。您最多可以同时使用42 种颜色,并使用配置文件来简化命令行。您可以为可以通过管道传输到另一个命令的每个命令的输出着色。如果您知道正则表达式是什么,那么使用. 您可以使用手册页来了解如何使用它。

回答by NVRM

The main tool for that is of course lolcat!

主要工具当然是lolcat

locate -bir pdf | lolcat

enter image description here

在此处输入图片说明

To install:

安装:

sudo apt install lolcat 

See man lolcatfor customizations.

请参阅man lolcat自定义。

回答by Bogdan Ratiu

Use the tput command.

使用 tput 命令。

Most terminals support 8 foreground text colors and 8 background colors (though some support as many as 256). Using the setaf and setab capabilities, we can set the foreground and background colors. The exact rendering of colors is a little hard to predict. Many desktop managers impose "system colors" on terminal windows, thereby modifying foreground and background colors from the standard. Despite this, here are what the colors should be:

大多数终端支持 8 种前景文本颜色和 8 种背景颜色(尽管有些支持多达 256 种)。使用 setaf 和 setab 功能,我们可以设置前景色和背景色。颜色的精确渲染有点难以预测。许多桌面管理器在终端窗口上强加了“系统颜色”,从而修改了标准中的前景色和背景色。尽管如此,这里的颜色应该是:

Value Color

值颜色

0 Black

0 黑色

1 Red

1 红色

2 Green

2 绿色

3 Yellow

3 黄色

4 Blue

4 蓝色

5 Magenta

5 洋红色

6 Cyan

6 青色

7 White

7 白色

8 Not used

8 未使用

9 Reset to default color

9 重置为默认颜色

Actual example: set color to red, cat and then change color back:

实际示例:将颜色设置为红色、猫,然后将颜色改回:

tput setaf 1; cat /proc/meminfo ; tput setaf 9