在UNIX命令行中回显字符

编程入门 行业动态 更新时间:2024-10-24 15:17:19
本文介绍了在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手册( zsh.sourceforge/Doc/Release/Shell-Builtin-Commands.html )

From The Z Shell Manual (zsh.sourceforge/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命令行中回显字符

本文发布于:2023-07-20 17:50:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169705.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令行   字符   UNIX   中回显

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!