我怎样才能回显
文字字符串-e
而不是别的?
我试图更好地理解 shell 参数是如何转义的。
以下命令不起作用:
echo -e # prints nothing
echo '-e' # prints nothing
echo "-e" # prints nothing
echo \-e # prints nothing
echo \\-e # prints \-e
echo '\-e' # prints \-e
echo "'-e'" # prints '-e' (with quotes)
echo -- -e # prints -- -e
我找不到既不包含引号也不包含前导斜线的。
最佳答案
How to echo the literal string "-e" (and nothing else) in bash?
printf '%s\n' '-e'
printf -- '-e\n'
来自 man echo -e
是一个选项:
-e
enable interpretation of backslash escapes
来自 bash builtins :
echo ...
If the -e option is given, interpretation of the following backslash-escaped characters is enabled.
...
echo does not interpret -- to mean the end of options.
因此 echo -e
将使 echo 将 -e
解释为标志并打印空换行符。
要打印 -e
,您基本上必须使用 printf
(或者您可以使用允许这样做的 echo
实现)。我不相信使用内置的 bash echo 只打印 -e
是可能的。
printf
更便携。有 echo
的实现不带 -e
参数,它可能会工作。在网上你可以找到various sites关于 echo 可移植性问题。
关于bash - 如何在 bash 中回显文字字符串 "-e"(仅此而已)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54102155/