如何回显文字字符串"-e" (什么都没有)在bash中? [英] How to echo the literal string "-e" (and nothing else) in bash?

查看:110
本文介绍了如何回显文字字符串"-e" (什么都没有)在bash中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能echo文字字符串-e而不是其他内容?

How can I echo the literal string -e and nothing else?

我试图更好地了解如何对shell参数进行转义.

I'm trying to better understand how shell arguments are escaped.

以下命令不起作用:

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

我找不到一个既不包含引号也不包含斜杠的内容.

I can't find one that doesn't either include quotes or a leading slash.

推荐答案

我假设真正的问题是为什么:

I'm assuming the real question is why:

根据您是以foo(4)还是foo(2+2)foo((int)16/4)调用它,C#或Java函数的行为永远不会有所不同,因为该信息在函数运行时就消失了.它可以告诉您通过了什么,但无法告诉您如何通过.

A C# or Java function can never behave differently based on whether you invoked it as foo(4) or foo(2+2) or foo((int)16/4) because that information is gone by the time the function runs. It can tell what you passed, but it can't tell how you passed it.

出于相同的原因,根据是否或如何转义其参数,命令的行为永远不会有所不同.引用和转义是一种 how ,而生成的字符串参数是 what .

For the same reason, a command can never behave differently based on whether or how you escaped its arguments. Quoting and escaping is a how, while the resulting string argument is the what.

这是您每次尝试的等效execlp调用(echo内置在bash中,但行为相同):

Here is the equivalent execlp call of each of your attempts (echo is builtin in bash but behaves the same):

#    v-- How you passed it            v-- What you passed
echo -e     # execlp("echo", "echo", "-e", 0);   # prints nothing
echo '-e'   # execlp("echo", "echo", "-e", 0);   # prints nothing
echo "-e"   # execlp("echo", "echo", "-e", 0);   # prints nothing
echo \-e    # execlp("echo", "echo", "-e", 0);   # prints nothing
echo \\-e   # execlp("echo", "echo", "\\-e", 0); # prints \-e
echo '\-e'  # execlp("echo", "echo", "\\-e", 0); # prints \-e
echo "'-e'" # execlp("echo", "echo", "'-e'", 0); # prints '-e' (with quotes)
echo -- -e  # execlp("echo", "echo", "--", "-e", 0);   # prints -- -e

如您所见,如何根本不影响输出.一切都归结为什么.这就是为什么世界上所有逃避都不会产生预期效果的原因.

As you can see, the how doesn't affect output at all. It all comes down to the what. This is why all the escaping in the world will fail to have the intended effect.

这篇关于如何回显文字字符串"-e" (什么都没有)在bash中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆