开发者

'echo' newline suppression [duplicate]

This question already has answers here: "echo -n" prints "-n" (11 answers) Closed 5 years ago.

Why doesn't $echo '-n' write -n on terminal although -n开发者_C百科 is written within single quotes?


Because the quotes are processed by the shell and the echo command receives plain -n. If you want to echo -n, you can e.g. printf '%s\n' -n


I found that the following just works in bash:

echo -n PRINT_THIS

So, you should put -n the first place.


You could try

echo -e '\055n'


You should try to use the more portable printf as far as possible.


The quotes don't help because ... ugh, it's hard to explain. The shell strips the quotes off before the "echo" command itself is evaluated, so by that time they don't matter.

Try this:

echo - -n

That's not documented as working (I'm running Ubuntu Linux), but echo is almost certainly a built-in to whatever shell you're using, so the man page is questionable anyway. It does work for me. (I'm running zsh because I'm really suave and sophisticated).

edit: well it seems that the bash builtin edit doesn't behave that way. I don't know what to suggest; this:

echo '' -n

will give you "-n" preceded by a space, which (depending on what you're doing) might be OK.


Not quite there:

echo -e 'a-n\b\b\b '
 -n

Surprisingly, this works:

echo -e '-n\b'
-n

And here is a solution I can explain:

echo "foobar" | sed 's/.*/-n/'
-n
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜