How do I escape the wildcard/asterisk character in bash?

andyuk picture andyuk · Sep 19, 2008 · Viewed 115.8k times · Source

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?

Answer

finnw picture finnw · Sep 19, 2008

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