回声" -n"不会打印-n? [英] echo "-n" will not print -n?

查看:101
本文介绍了回声" -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.

修改:从<一个href=\"http://stackoverflow.com/questions/13042734/parsing-command-line-arguments-in-bash-strange-behavior/13042777#13042777\">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")

这篇关于回声&QUOT; -n&QUOT;不会打印-n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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