我该如何回应" -e"? [英] How do I echo "-e"?

查看:215
本文介绍了我该如何回应" -e"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想回声可能包含在参数相同字符串回声 。我怎样才能做到这一点无需修改字符串?

I want to echo a string that might contain the same parameters as echo. How can I do it without modifying the string?

例如:

$ var="-e something"
$ echo $var
something

...没有打印 -e

推荐答案

一个令人惊异的问题。既然你标记庆典,我会假设你的意思内部的回声命令,虽然GNU的coreutils庆典独立回声命令可能工作方式类似于就够了。

A surprisingly deep question. Since you tagged bash, I'll assume you mean bash's internal echo command, though the GNU coreutils' standalone echo command probably works similarly enough.

它的要点是:如果你的真正的需要使用回声(这将是令人惊讶的,但是这是问题的写入方法现在),这一切都取决于究竟你的字符串可以包含

The gist of it is: if you really need to use echo (which would be surprising, but that's the way the question is written by now), it all depends on what exactly your string can contain.

在这种情况下,你需要做的就是将它传​​递给之前引用变量回声

In that case, all you need to do is quote the variable before passing it to echo.

$ var="-e something"
$ echo "$var"
-e something

如果该字符串不是一个eaxctly 回声选项或组合,包括任何非选项的后缀,它不会被<$ C $承认的C>回声,将被打印出来。

If the string isn't eaxctly an echo option or combination, which includes any non-option suffix, it won't be recognized as such by echo and will be printed out.

如果你的情况可以减少只是-e,它得到棘手。做到这一点的一种方法是:

If your case can reduce to just "-e", it gets trickier. One way to do it would be:

$ echo -e '\055e'
-e

(逸出破折号所以它不会PTED作为一个选项,但作为八进制序列间$ P $)

(escaping the dash so it doesn't get interpreted as an option but as on octal sequence)

这是改写字符串。它的可以的自动和非破坏性完成的,所以感觉可以接受的:

That's rewriting the string. It can be done automatically and non-destructively, so it feels acceptable:

$ var="-e something"
$ echo -e ${var/#-/\\055}
-e something

您,我实际使用 -e 选项间preT八进制序列,所以如果你打算<$ C,它不会工作注意到$ C>回声-E 。它会为其他的选择工作,虽然。

You noticed I'm actually using the -e option to interpret an octal sequence, so it won't work if you intended to echo -E. It will work for other options, though.

说真的,你不局限于回声,是你吗?

Seriously, you're not restricted to echo, are you?

printf '%s\n' "$var"

这篇关于我该如何回应&QUOT; -e&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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