bash - 如何在 bash 中回显文字字符串 "-e"(仅此而已)?

标签 bash escaping

我怎样才能回显文字字符串-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/

相关文章:

html - 应该 &'s be escaped in onclick="...”?

json - 在 Perl Mojolicious 中渲染 JSON 时防止转义字符

bash - 将标准输出重定向到新文件会在 cygwin 中的文件名末尾添加奇怪的字符

bash - 用于在 postgres 中创建带有密码的用户的 shell 脚本失败并带有引号

bash - ls $FOLDER_PATH $FOLDER_PATH : No such file or directory 中有空格

bash - 排序命令无法正常工作,错误或功能?

bash - 如何检查转换为小写的字符串是否等于 Bash 代码中的内容?

python - 用于与特殊转义规则匹配的字符串的正则表达式

javascript - 如何使用 javascript 转义 eval 中的特殊字符?

groovy - 在 groovy 中,如何在不带转义斜杠 (\) 且不带插值的情况下分配多行字符串