Echo 'The Character - (Dash) in the Unix Command Line

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

7

3 Answers

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 ()

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.

Try this on zsh:

[asatsi@stevevai]~% echo - -
-
1

echo - works on my system. Linux and bash.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest