echo newline suppression [duplicate]

怎甘沉沦 提交于 2019-12-22 04:44:19

问题


Why doesn't $echo '-n' write -n on terminal although -n is written within single quotes ?


回答1:


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




回答2:


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




回答3:


You could try

echo -e '\055n'



回答4:


I found that the following just works in bash:

echo -n PRINT_THIS

So, you should put -n the first place.




回答5:


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.




回答6:


Not quiete there:

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

surprisingly this works:

echo -e '-n\b'
-n

and here a solution I can explain:

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


来源:https://stackoverflow.com/questions/2645174/echo-newline-suppression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!