如何让 bash shell 显示彩色文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18560647/
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
how to make bash shell display colorful text?
提问by user2268624
I saw someone can make the output in bash shell to display colorfully. Not only highlight the error or warning, but also the directory when execute 'ls'.
我看到有人可以在 bash shell 中制作输出以彩色显示。不仅突出显示错误或警告,还突出显示执行 'ls' 时的目录。
Does anyone can tell me how to make it? Thanks a lot.
有谁可以告诉我如何制作吗?非常感谢。
回答by Rahul Tripathi
You can check out thisand this:-
Use ANSI escape sequences to set text properties like foreground and background colors.
使用 ANSI 转义序列设置前景色和背景色等文本属性。
EXAMPLE:-
例子:-
echo -e "\e[1;34mThis is a blue text.\e[0m"
and
和
#!/bin/bash
# tputcolors
echo
echo -e "$(tput bold) reg bld und tput-command-colors$(tput sgr0)"
for i in $(seq 1 7); do
echo " $(tput setaf $i)Text$(tput sgr0) $(tput bold)$(tput setaf $i)Text$(tput sgr0) $(tput sgr 0 1)$(tput setaf $i)Text$(tput sgr0) $(tput setaf $i)"
done
echo ' Bold $(tput bold)'
echo ' Underline $(tput sgr 0 1)'
echo ' Reset $(tput sgr0)'
echo
回答by rici
In the specific case of ls
:
在以下特定情况下ls
:
If you use a linux distro, almost all of which come with gnu ls
, you probably already have coloured output. If you don't, try ls -C
or ls --color=auto
. You may need to set $LS_COLORS
; see man dircolors
for a way to do it easily.
如果您使用 linux 发行版,几乎所有发行版都带有 gnu ls
,那么您可能已经有了彩色输出。如果没有,请尝试ls -C
或ls --color=auto
。您可能需要设置$LS_COLORS
;看看man dircolors
有什么方法可以轻松完成。
But probably you are on Mac OS X, which uses a different version of ls
. If your terminal outputs colors, you can probably get colored ls
output with ls -G
, but again you may need to set $LSCOLORS
. Also, you may need to export CLICOLOR=1
in order to get the terminal to show colours. For more information, see this answeron a companion site.
但可能您使用的是 Mac OS X,它使用不同版本的ls
. 如果您的终端输出颜色,您可能可以使用 获得彩色ls
输出ls -G
,但您可能需要再次设置$LSCOLORS
. 此外,您可能需要export CLICOLOR=1
让终端显示颜色。有关更多信息,请参阅配套站点上的此答案。
回答by Ziffusion
Here's a little bit of background, if you are interested.
如果您有兴趣,这里有一些背景知识。
As others have pointed out, the terminals respond to what are called escape sequences. These are sequence of characters that you can send to the terminal (write, display), and instead of displaying these characters, the terminal hardware interprets them as commands to do various things. These things could be anything from moving the cursor, to blinking, to changing the foreground and background colors. This is what ANY terminal program does (be it vi, emacs, ls, or bash) when it needs to invoke certain terminal capabilities.
正如其他人指出的那样,终端响应所谓的转义序列。这些是您可以发送到终端(写入、显示)的字符序列,终端硬件将它们解释为执行各种操作的命令,而不是显示这些字符。这些东西可以是任何东西,从移动光标到闪烁,再到改变前景色和背景色。这就是任何终端程序(无论是 vi、emacs、ls 还是 bash)在需要调用某些终端功能时所做的事情。
The problem is that there are many different kinds of terminals, that have varying capabilities, and respond to different escape sequences. Early on, people came up with a mechanism to deal with this. They came up with termcap, and then terminfo, which is a way to abstract the terminal capabilities. So, you do things like "move cursor", or "set foreground color", and the underlying library retrieves the correct escape sequence from a DB maintained for different terminals (based on the value of TERM environment variable, etc.).
问题是有许多不同类型的终端,它们具有不同的功能,并响应不同的转义序列。早期,人们想出了一种机制来解决这个问题。他们提出了 termcap,然后是 terminfo,这是一种抽象终端功能的方法。因此,您执行诸如“移动光标”或“设置前景色”之类的操作,底层库从为不同终端维护的数据库中检索正确的转义序列(基于 TERM 环境变量的值等)。
Thus you have the famous curses library. It lets you do all this. For things like bash, they have encapsulated this functionality in a command you can invoke, called tput. The man page give you the detailsof how to invoke it. So, it would be preferable to not use escape sequences directly. But rather you should go through tput so that you remain independent of the terminal you happen to be working with.
这样你就有了著名的诅咒库。它让你做这一切。对于像 bash 这样的东西,他们将这个功能封装在一个你可以调用的命令中,称为 tput。该手册页给你详细的如何调用它。因此,最好不要直接使用转义序列。而是您应该通过 tput 以便您保持独立于您碰巧使用的终端。
回答by konsolebox
These things were around since the days of DOS. Use ANSI Escape Sequences or Codes:
这些东西从 DOS 时代就存在了。使用 ANSI 转义序列或代码:
http://wiki.bash-hackers.org/scripting/terminalcodes
http://wiki.bash-hackers.org/scripting/terminalcodes
Example usage:
用法示例:
This would print ABC in green text:
这将以绿色文本打印 ABC:
ESCAPE=$'\e'
echo "${ESCAPE}[0;32mABC${ESCAPE}[0m"
You could also use -e option:
您还可以使用 -e 选项:
echo -e "\e[0;32mABC\e[0m"
Or printf:
或 printf:
printf "\e[0;32mABC\e[0m\n"
Check out some of the codes I mainly use in scripts here.
在这里查看我主要在脚本中使用的一些代码。