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?

2

1 Answer 1

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" ;
1
  • 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. Feb 24, 2021 at 10:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .