回声" -n"不会打印-n?

回声" -n"不会打印-n?

问题描述:

非常相似这个问题

我通过几件事迭代与BASH自动化脚本。有时,脚本会遇到-n和echo将尝试间preT这一点。

I'm iterating through a few things with an automated script in BASH. Occasionally the script will come across "-n" and echo will attempt to interpret this.

此尝试:

 $ POSIXLY_CORRECT=1 /bin/echo -n

 $ POSIXLY_CORRECT=1 /bin/echo "-n"

但是,除$ P $每次PTED的说法。

But it interpreted the argument each time.

然后这一点,这工作,但它可能在弦打转义字符,这就是为什么我不想空字符适用于所有的输入,并使用-e。

Then this, which works but it's possible to hit escaped characters in the strings, which is why I don't want to apply a null character to all input and use -e.

$ echo -e "\x00-n"
-n

printf的是可能的,但是要避免的,除非没有其他选项(并非所有的机器都printf的作为实用程序)。

printf is possible, but is to be avoided unless there are no other options (Not all machines have printf as a utility).

$printf "%s" "-n"
-n

那么,有没有办法让回声打印-n?

So is there a way to get echo to print "-n"?

的printf 应该是一个内置于所有的炮弹,除非你的一些机器有很老外壳的版本。这是一个内置的庆典很长一段时间。这也可能是更便携比回声-e

printf should be a built-in in all your shells, unless some of your machines have very old shell versions. It's been a built-in in bash for a long time. It's probably more portable than echo -e.

否则,真的没有办法让回声选项它关心。

Otherwise, there's really no way to get echo options it cares about.

修改:从an回答另一个类似的问题;避免使用这个方便的无数字版权保留包装报价与的printf 问题:

Edit: from an answer to another similar question; avoid quoting issues with printf by using this handy no-digital-rights-retained wrapper:

ech-o() { printf "%s\n" "$*"; }

(这是ECH-O,如不带选项)

(That's ech-o, as in "without options")