How to expand variables in bash variable without template extension?

I have a variable that contains a line like this:

var='$FOO/bar/baz*'

and I want to replace the $ FOO variable with its contents. However when i do

var=$(eval "echo $var")

This variable is replaced, but the star is also replaced so that it varnow contains all possible matches in my file system (as if I clicked a tab in the shell). for example, if $ FOO contains / home, it varwill contain"/home/bar/baz1.sh /home/bar/baz2.sh /home/bar/baz.conf"

How to replace a variable without wildcard expansion?

+5
source share
2 answers

Disable the globe in bash and then turn it back on.

set -f 
var="$FOO/bar/baz*"
set +f
+5
source

Just drop the quotation marks:

var=$FOO/bar/baz/*

Globs do not expand on RHS variable assignment.

+1
source

All Articles