Lazy Evaluation in Bash
One safe possibility is to use a function:
expand_pattern() {
pattern="$x and $y"
}
That's all. Then use as follows:
x=1 y=1
expand_pattern
echo "$pattern"
You can even use x
and y
as environment variables (so that they are not set in the main scope):
x=1 y=1 expand_pattern
echo "$pattern"
You can use the command envsubst from gettext, for example:
$ pattern='x=$x and y=$y'
$ x=1 y=2 envsubst <<< $pattern
x=1 and y=2