bashshellescaping

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


For example:

FOO="BAR * BAR"
echo $FOO

Output:

BAR file1 file2 file3 file4 BAR

And using the \ escape character:

FOO="BAR \* BAR"
echo $FOO

Output:

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