echo 'the character - (dash) in the unix command line

若如初见. 提交于 2020-03-02 07:57:15

问题


This is an extremely simple question that had absolutely nothing related on the internet no matter what. How do I print a single dash with the echo command? I tried escaping it with '\', '/', '-', '%' and even '#', but it seemed it refused to print a simple, single dash character nonetheless. I tried it with a single quote, double quote or no quote at all, but it seems that this simply isn't possible with echo. Nothing on the man page nor anywhere else, for that matter, can someone please tell me how to do this? To make things absolutely clear: I want to use echo '-' (or anything like this ) and get a single dash - as output


回答1:


This question was asked more than 2 years ago, but I've just noticed that echo on bash and zsh behaves differently.

On Zsh, the first dash is used to terminate option processing; so as @asatsi answered, you can print - with

% echo - -
-

On Bash, single dash works just fine.

$ echo -
-

From The Z Shell Manual (http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html)

echo [ -neE ] [ arg ... ]

...

Note that for standards compliance a double dash does not terminate option processing; instead, it is printed directly. However, a single dash does terminate option processing, so the first dash, possibly following options, is not printed, but everything following it is printed as an argument. The single dash behaviour is different from other shells. For a more portable way of printing text, see printf, and for a more controllable way of printing text within zsh, see print.




回答2:


Try this on zsh:

[asatsi@stevevai]~% echo - -
-



回答3:


echo - works on my system. Linux and bash.



来源:https://stackoverflow.com/questions/44448096/echo-the-character-dash-in-the-unix-command-line

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