How to use an environment variable inside a quoted string in linux/bash

136 views Asked by At

In openfoam, I use foamDictionary to set a string ( 800 "patch" ) for entry timeVsFile in ./controlDict file. As shown, this string includes a double quote.

I define an environment variable a to represent the value 800

a=800
foamDictionary ./controlDict -entry timeVsFile -set '( $a "patch" )'

The correct result should be timeVsFile (800 "patch");

But I get timeVsFile ($a "patch");

Can anyone help me to solve this problem?

Thanks!

1

There are 1 answers

1
Itagaki Fumihiko On BEST ANSWER

Variables are not expanded in single quote.

a=800
echo '($a)'

Output:

($a)

To expand the variables within quoting, use double quote.

echo '('"$a"')'

Output:

(800)

It's a jumbled mess, but look at it broken down into '(', "$a" and ')'.

So,

'( $a "patch" )'

this could be done as follows.

'( '"$a"' "patch" )'