bash 如何为 PHP CLI 启用颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34034730/
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 enable color for PHP CLI?
提问by Barbayar Dashzeveg
How do I enable the colors for output of CLI? The below one is, running on Ubuntu.
如何启用 CLI 输出的颜色?下面的一个是,在 Ubuntu 上运行。
If you see the screenshot, obviously the colors is enabled for terminal. And, if I call echo
, it doesn't colorize the result, but if I use echo -e
, it colorizes.
I checked manual page of echo
, and -e
means enable interpretation of backslash escapes
How can I enable this option for PHP CLI?
如果您看到屏幕截图,显然终端启用了颜色。而且,如果我调用echo
,它不会为结果着色,但如果我使用echo -e
,它会着色。
我检查了手册页echo
,并-e
表示启用反斜杠转义的解释
如何为 PHP CLI 启用此选项?
回答by jayxhj
First we use an escape character so we can actually define a output color. This is done with \033
(\e). Then we open the color statement with [31m
. Red in this case.
首先我们使用转义字符,这样我们就可以实际定义输出颜色。这是通过\033
(\e) 完成的。然后我们用[31m
.打开颜色语句。在这种情况下是红色的。
The “some colored text” will be the text outputted in a different color. And after that we have to close the color statement with \033[0m
.
“一些彩色文本”将以不同颜色输出的文本。之后,我们必须用 关闭颜色声明\033[0m
。
php -r 'echo "3[31m some colored text 3[0m some white text \n";'