How do I escape the wildcard/asterisk character in bash?
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
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:
- Parameter expansion
- 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
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