2

How to pass the variable value in echo statement?

testvar="actualvalue"
echo 'testing "${testvar}", "testing", "testing" ;'

Expected output:

testing "actualvalue", "testing", "testing" ;

But, I am getting the below output:

testing "${testvar}", "testing", "testing" ;

Can someone help me with this?

Dev
  • 123

1 Answers1

4

Remove the single quotes:

$ testvar="actualvalue"
$ echo testing "${testvar}", "testing", "testing" ;
testing actualvalue, testing, testing

The single-quote inhibits variable expansion.

Without double-quotes gives you the same output:

$ echo testing ${testvar}, testing, testing ;
testing actualvalue, testing, testing

But if you really want double-quotes in the output, escape them:

$ echo "testing \"${testvar}\", \"testing\", \"testing\" ;"
testing "actualvalue", "testing", "testing" ;
GAD3R
  • 66,769
Stewart
  • 13,677
  • 1
    ${testvar} outside double quotes is invoking the split+glob operator, so it would only give the same output in the special cases where ${testvar} contains no wildcard characters nor characters of $IFS. Also note that echo can generally not be used to output arbitrary data. You'd use printf for that instead. – Stéphane Chazelas Feb 24 '21 at 10:14