Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 如何使用echo命令输出颜色转义序列_Bash - Fatal编程技术网

Bash 如何使用echo命令输出颜色转义序列

Bash 如何使用echo命令输出颜色转义序列,bash,Bash,我希望它能以绿色字母输出www.google.com 相反,我得到了 -e\e[1;34mwww.google.com\e[0m根据使用的环境或外壳可能产生的影响,您可能会做的一件事是使用ANSI-C引用: domain="www.google.com" echo -e "\e[1;34m"$domain"\e[0m" 对$'string'形式的单词进行了特殊处理。该单词扩展为 字符串,并按指定替换反斜杠转义字符 ANSI C标准 如果使用sh script.sh运行脚本,则显式使用sh作为s

我希望它能以绿色字母输出
www.google.com

相反,我得到了


-e\e[1;34mwww.google.com\e[0m

根据使用的环境或外壳可能产生的影响,您可能会做的一件事是使用ANSI-C引用:

domain="www.google.com"
echo -e "\e[1;34m"$domain"\e[0m"
对$'string'形式的单词进行了特殊处理。该单词扩展为 字符串,并按指定替换反斜杠转义字符 ANSI C标准


如果使用
sh script.sh
运行脚本,则显式使用
sh
作为shell,而不是shebang行中的shell。如果
sh
不是指向
bash
的链接,这是个坏消息。普通
sh
shell可能不支持
echo-e


键入
/script.sh
使用shebang行中的解释器。

您的shell真的是bash吗?我怀疑不是。如果这是脚本,请将
#!/bin/bash
放在顶部。@JohnKugelman是的,shebang已经在顶部了。您如何运行它?
/script.sh
sh script.sh
?不要做后者。拿铁咖啡r~!!!它在我的Ubuntu、Kali、Backtract和Parrot上也能正常工作。你能告诉我你正在使用哪个版本的Linux吗?虽然这是一个有效的答案,但我选择了ILI的答案,因为它直接允许我显示颜色,而无需改变我运行脚本的方式。@user299709如果使用
bash
-特定功能。不过,您应该更改运行脚本的方式。@user299709:另一种选择是使用转义码
\033
而不是
\e
(例如
echo-e'\033[1;34m'${domain}'\033[0m'
)…有些shell不使用
\e
,在这种情况下,通常必须使用另一种转义字符的方法。
echo $'\e[1;34m'${domain}$'\e[0m'