Bash: echo string that starts with "-"

Viewed 11745
VAR="-e xyz"
echo $VAR

This prints xyz, for some reason. I don't seem to be able to find a way to get a string to start with -e.

What is going on here?

4 Answers

In zsh, you can use a single dash (-) before your arguments. This ensures that no following arguments are interpreted as options.

% VAR="-e xyz"
% echo - $VAR
-e xyz

From the zsh docs:

   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.

Keep in mind this behavior is specific to zsh.

Related