Again, as often, it's not the value actually in the variable, but how the variable is. The echo
in this case. Zsh's echo
takes the single dash as an end of options indicator, so it's removed. Online manual:
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.
So we have:
zsh% echo -
zsh% echo - -n
-n
zsh% var=-
zsh% printf "%s\n" "$var"
-
See also:
zsh
. – schily Sep 08 '18 at 15:18echo
builtin in Zsh, you can always use, i.e.,command echo $var
instead ofecho $var
and your dash will be printed. – user1934428 Sep 10 '18 at 08:46echo
will not necessarily help. For instance, on GNU systems, you'll still have problems for values of$var
like-n
,--version
,-Ene
... Inzsh
,echo -E - $var
,print -r -- $var
andprintf '%s\n' "$var"
should be equivalent but only the latter one is portable to other Bourne-like shells which is why zsh and POSIX recommend to useprintf
here. Only zsh and yashecho
s are able to output arbitrary data. – Stéphane Chazelas Sep 10 '18 at 10:09/bin/echo -
outputs does output a dash. Right now, I also have access to some (somewhat older) Linux, and here too, it works. Could you cite a source which would explain why my approach wouldn't work? – user1934428 Sep 10 '18 at 16:02echo -
handling is specific tozsh
(some old versions of pdksh output nothing onecho -
but that was a bug then). I was pointing out that GNUecho
(and most otherecho
implementations) have issues of their own, not on-
but other values of$var
like--version
,-nene
... So using anotherecho
in general is not the best solution. – Stéphane Chazelas Sep 10 '18 at 16:07