3

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
  • 3
    The single quotes in the echo command remove the special meaning of $. Replace the single quotes with double quotes, and put backslashes before the other double quotes: echo "testing \"${testvar}\", \"testing\", \"testing\" ;". Commented Feb 24, 2021 at 7:41
  • 2
    ... see for example What is the difference between “…”, '…', $'…', and $“…” quotes? Commented Feb 24, 2021 at 7:46

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. Commented Feb 24, 2021 at 10:14

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.