168

For example:

me$ FOO="BAR * BAR"
me$ echo $FOO
BAR file1 file2 file3 file4 BAR

and using the \ escape character:

me$ FOO="BAR \* BAR"
me$ echo $FOO
BAR \* BAR

I'm obviously doing something stupid.

How do I get the output BAR * BAR?

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
andyuk
  • 38,118
  • 16
  • 52
  • 52

7 Answers7

175

Quoting when setting $FOO is not enough. You need to quote the variable reference as well:

me$ FOO="BAR * BAR"
me$ echo "$FOO"
BAR * BAR
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
finnw
  • 47,861
  • 24
  • 143
  • 221
126

SHORT ANSWER

Like others have said - you should always quote the variables to prevent strange behaviour. So use echo "$foo" in instead of just echo $foo.

LONG ANSWER

I do think this example warrants further explanation because there is more going on than it might seem on the face of it.

I can see where your confusion comes in because after you ran your first example you probably thought to yourself that the shell is obviously doing:

  1. Parameter expansion
  2. Filename expansion

So from your first example:

me$ FOO="BAR * BAR"
me$ echo $FOO

After parameter expansion is equivalent to:

me$ echo BAR * BAR

And after filename expansion is equivalent to:

me$ echo BAR file1 file2 file3 file4 BAR

And if you just type echo BAR * BAR into the command line you will see that they are equivalent.

So you probably thought to yourself "if I escape the *, I can prevent the filename expansion"

So from your second example:

me$ FOO="BAR \* BAR"
me$ echo $FOO

After parameter expansion should be equivalent to:

me$ echo BAR \* BAR

And after filename expansion should be equivalent to:

me$ echo BAR \* BAR

And if you try typing "echo BAR \* BAR" directly into the command line it will indeed print "BAR * BAR" because the filename expansion is prevented by the escape.

So why did using $foo not work?

It's because there is a third expansion that takes place - Quote Removal. From the bash manual quote removal is:

After the preceding expansions, all unquoted occurrences of the characters ‘\’, ‘'’, and ‘"’ that did not result from one of the above expansions are removed.

So what happens is when you type the command directly into the command line, the escape character is not the result of a previous expansion so BASH removes it before sending it to the echo command, but in the 2nd example, the "\*" was the result of a previous Parameter expansion, so it is NOT removed. As a result, echo receives "\*" and that's what it prints.

Note the difference between the first example - "*" is not included in the characters that will be removed by Quote Removal.

I hope this makes sense. In the end the conclusion in the same - just use quotes. I just thought I'd explain why escaping, which logically should work if only Parameter and Filename expansion are at play, didn't work.

For a full explanation of BASH expansions, refer to:

http://www.gnu.org/software/bash/manual/bashref.html#Shell-Expansions

Carl G
  • 17,394
  • 14
  • 91
  • 115
mithu
  • 1,273
  • 1
  • 7
  • 5
  • 1
    Great answer! Now I don't feel I asked such a dumb question. :-) – andyuk Sep 19 '08 at 18:12
  • 1
    There exist some utility to escape the special characters ? – jmj Aug 30 '12 at 07:47
  • "you should always quote the variables to prevent strange behaviour" -- when you want to use them as strings – Angelo Oct 21 '15 at 15:24
  • See also http://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-variable – tripleee Feb 06 '17 at 10:31
  • While the answer from @finnw up above does directly answer the question, this answer is much better at explaining the _why_ of it, which helps much more in extrapolating how using in our own scenarios would work. This is the kind of answer we need to see more of. – Nicolas Coombs Nov 19 '19 at 15:10
65

I'll add a bit to this old thread.

Usually you would use

$ echo "$FOO"

However, I've had problems even with this syntax. Consider the following script.

#!/bin/bash
curl_opts="-s --noproxy * -O"
curl $curl_opts "$1"

The * needs to be passed verbatim to curl, but the same problems will arise. The above example won't work (it will expand to filenames in the current directory) and neither will \*. You also can't quote $curl_opts because it will be recognized as a single (invalid) option to curl.

curl: option -s --noproxy * -O: is unknown
curl: try 'curl --help' or 'curl --manual' for more information

Therefore I would recommend the use of the bash variable $GLOBIGNORE to prevent filename expansion altogether if applied to the global pattern, or use the set -f built-in flag.

#!/bin/bash
GLOBIGNORE="*"
curl_opts="-s --noproxy * -O"
curl $curl_opts "$1"  ## no filename expansion

Applying to your original example:

me$ FOO="BAR * BAR"

me$ echo $FOO
BAR file1 file2 file3 file4 BAR

me$ set -f
me$ echo $FOO
BAR * BAR

me$ set +f
me$ GLOBIGNORE=*
me$ echo $FOO
BAR * BAR
Alex Just Alex
  • 801
  • 6
  • 4
6
FOO='BAR * BAR'
echo "$FOO"
tzot
  • 92,761
  • 29
  • 141
  • 204
  • This works, but it's not essential to change the single quotes on the first line to double quotes. – finnw Sep 19 '08 at 14:08
  • 1
    No, it's not, but the habit to use single quotes is preferable when you're going to include special shell characters and you don't want any substitution. – tzot Sep 19 '08 at 14:10
4

If you don't want to bother with weird expansions from bash you can do this

me$ FOO="BAR \x2A BAR"   # 2A is hex code for *
me$ echo -e $FOO
BAR * BAR
me$ 

Explanation here why using -e option of echo makes life easier:

Relevant quote from man here:

SYNOPSIS
   echo [SHORT-OPTION]... [STRING]...
   echo LONG-OPTION

DESCRIPTION
   Echo the STRING(s) to standard output.

   -n     do not output the trailing newline

   -e     enable interpretation of backslash escapes

   -E     disable interpretation of backslash escapes (default)

   --help display this help and exit

   --version
          output version information and exit

   If -e is in effect, the following sequences are recognized:

   \\     backslash

   ...

   \0NNN  byte with octal value NNN (1 to 3 digits)

   \xHH   byte with hexadecimal value HH (1 to 2 digits)

For the hex code you can check man ascii page (first line in octal, second decimal, third hex):

   051   41    29    )                           151   105   69    i
   052   42    2A    *                           152   106   6A    j
   053   43    2B    +                           153   107   6B    k
INS
  • 10,594
  • 7
  • 58
  • 89
3
echo "$FOO"
Rafał Dowgird
  • 43,216
  • 11
  • 77
  • 90
3

It may be worth getting into the habit of using printf rather then echo on the command line.

In this example it doesn't give much benefit but it can be more useful with more complex output.

FOO="BAR * BAR"
printf %s "$FOO"
David Webb
  • 190,537
  • 57
  • 313
  • 299
  • Why in hell? printf is a separate process (at least, not a built-in in bash) and the use of printf you demonstrate has no benefit over echo. – ddaa Sep 19 '08 at 14:35
  • 2
    printf *is* a built-in bash and I did say in my answer "In this example it doesn't give much benefit but it can be more useful with more complex output. – David Webb Oct 02 '08 at 10:50