Home > Software design >  How to use an environment variable inside a quoted string in linux/bash
How to use an environment variable inside a quoted string in linux/bash

Time:05-05

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!

CodePudding user response:

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" )'
  • Related