bashshellescaping

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


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?


Solution

  • 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