bash echo "-e" 不打印任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/320313/
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
echo "-e" doesn't print anything
提问by wheleph
I'm using GNU bash, version 3.00.15(1)-release (x86_64-redhat-linux-gnu). And this command:
我正在使用 GNU bash,版本 3.00.15(1)-release (x86_64-redhat-linux-gnu)。而这个命令:
echo "-e"
doesn't print anything. I guess this is because "-e" is one of a valid options of echo command because echo "-n" and echo "-E" (the other two options) also produce empty strings.
不打印任何东西。我猜这是因为“-e”是echo命令的有效选项之一,因为echo“-n”和echo“-E”(其他两个选项)也会产生空字符串。
The question is how to escape the sequence "-e" for echo to get the natural output ("-e").
问题是如何为 echo 转义序列“-e”以获得自然输出(“-e”)。
回答by nanaya
The one true way to print any arbitrary string:
打印任意字符串的一种真正方法:
printf "%s" "$vars"
回答by nanaya
This is a tough one ;)
这是困难的一个 ;)
Usually you would use double dashes to tell the command that it should stop interpreting options, but echo will only output those:
通常你会使用双破折号告诉命令它应该停止解释选项,但 echo 只会输出那些:
$ echo -- -e
-- -e
You can use -e itself to get around the problem:
您可以使用 -e 本身来解决问题:
$ echo -e '5e'
-e
Also, as others have pointed out, if you don't insist on using the bash builtin echo, your /bin/echobinary might be the GNU version of the tool (check the man page) and thus understand the POSIXLY_CORRECTenvironment variable:
此外,正如其他人指出的那样,如果您不坚持使用 bash builtin echo,您的/bin/echo二进制文件可能是该工具的 GNU 版本(查看手册页),从而了解POSIXLY_CORRECT环境变量:
$ POSIXLY_CORRECT=1 /bin/echo -e
-e
回答by Stephen Darlington
There may be a better way, but this works:
可能有更好的方法,但这有效:
printf -- "-e\n"
回答by Joachim Sauer
You could cheat by doing
你可以作弊
echo "-e "
That would be dash, e, space.
那将是破折号,e,空格。
Alternatively you can use the more complex, but more precise:
或者,您可以使用更复杂但更精确的:
echo -e \\x2De
回答by The Archetypal Paul
After paying careful attention to the man page :)
仔细阅读手册页后:)
SYSV3=1 /usr/bin/echo -e
works, on Solaris at least
工作,至少在 Solaris 上
回答by azerole
[root@scintia mail]# POSIXLY_CORRECT=1; export POSIXLY_CORRECT [root@scintia mail]# /bin/echo "-e" -e [root@scintia mail]#
回答by Tilman Vogel
I like that one using a herestring:
我喜欢使用 herestring 的那个:
cat <<<"-e"
回答by CesarB
Another alternative:
另一种选择:
echo x-e | sed 's/^x//'
This is the way recommended by the autoconf manual:
这是autoconf 手册推荐的方式:
[...] It is often possible to avoid this problem using 'echo "x$word"', taking the 'x' into account later in the pipe.
[...] 通常可以使用 'echo "x$word"' 来避免这个问题,稍后在管道中将 'x' 考虑在内。
回答by FerranB
Another way:
其它的办法:
echo -e' '
echo -e " \b-e"
回答by wheleph
/bin/echo -e
works, but why?
有效,但为什么呢?
[resin@nevada ~]$ which echo
/bin/echo

