Search code examples
linuxecho

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


Solution

  • 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.