andyuk andyuk - 6 months ago 21
Bash Question

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

eg.

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

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
Comments