36
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?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
miracle2k
  • 29,597
  • 21
  • 65
  • 64

4 Answers4

60

The answers that say to put $VAR in quotes are only correct by side effect. That is, when put in quotes, echo(1) receives a single argument of -e xyz, and since that is not a valid option string, echo just prints it out. It is a side effect as echo could just as easily print an error regarding malformed options. Most programs will do this, but it seems GNU echo (from coreutils) and the version built into bash simply echo strings that start with a hyphen but are not valid argument strings. This behaviour is not documented so it should not be relied upon.

Further, if $VAR contains a valid echo option argument, then quoting $VAR will not help:

$ VAR="-e"
$ echo "$VAR"

$

Most GNU programs take -- as an argument to mean no more option processing — all the arguments after -- are to be processed as non-option arguments. bash echo does not support this so you cannot use it. Even if it did, it would not be portable. echo has other portability issues (-n vs \c, no -e).

The correct and portable solution is to use printf(1).

printf "%s\n" "$VAR"
camh
  • 40,988
  • 13
  • 62
  • 70
  • Mostly correct answer. Although POSIX standard clearly says "Implementations shall not support any options", historically part of implementations do support options (violating the standard). So `printf` is the right solution, `echo` should really be used to output some constant strings like "Hello world" which you know do not contain dashes or backslashes. – Roman Cheplyaka Sep 08 '10 at 05:21
  • Great answer. Been trying to figure this out for a bit now. thank you. – Todd Partridge 'Gen2ly' May 25 '12 at 23:23
  • If you are hell-bent on using `echo`, you can use escape sequences `echo -e '\055e'`. – dosentmatter Feb 15 '18 at 19:34
0

Try:

echo "$VAR"

instead.

(-e is a valid option for echo - this is what causes this phenomenon).

adamk
  • 45,184
  • 7
  • 50
  • 57
0

The variable VAR contains -e xyz, if you access the variable via $ the -e is interpreted as a command-line option for echo. Note that the content of $VAR is not wrapped into "" automatically.

Use echo "$VAR" to fix your problem.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
heb
  • 745
  • 4
  • 7
0

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.

Arminius
  • 2,363
  • 1
  • 18
  • 22