在UNIX命令行中回显字符-(破折号) [英] echo 'the character - (dash) in the unix command line

查看:79
本文介绍了在UNIX命令行中回显字符-(破折号)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的问题,无论如何,互联网上都没有任何关系。
如何使用echo命令打印单个破折号?我尝试使用'with','/','-','%'甚至'#'进行转义,但它似乎仍然拒绝打印简单的单破折号。我尝试使用单引号,双引号或根本没有引号,但是似乎通过echo无法实现。手册页上或其他任何地方都没有,有人可以告诉我该怎么做吗?
要使事情变得完全清楚:我想使用echo'-'(或类似的东西)并得到一个破折号-作为输出

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

推荐答案

这个问题是两年多以前问的,但我刚刚注意到,bash和zsh上的 echo 表现不同。

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

在Zsh上,第一个破折号用于终止选项处理;因此,正如@asatsi回答的那样,您可以使用

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

% echo - -
-

在Bash上,单破折号仅适用于

On Bash, single dash works just fine.

$ echo -
-

来自Z Shell手册( http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html

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


echo [-neE] [arg ...]

echo [ -neE ] [ arg ... ]

...

请注意,为了符合标准,双破折号不会终止选项处理;而是直接打印。但是,单个破折号会终止选项处理,因此不会打印第一个破折号(可能跟在选项之后),而是将其后的所有内容打印为参数。单破折号行为不同于其他外壳。有关打印文本的更简便方法,请参见printf,有关在zsh中打印文本的更可控方法,请参见print。

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.

这篇关于在UNIX命令行中回显字符-(破折号)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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