2

Scenario

$ VAR1=test

$ VAR2=testing

$ VAR3=$VAR1_$VAR2

$ echo $VAR3

testing

I expected "test_testing" as the output. Why its not working? How to output in "test_testing" format? (make $VAR1_$VAR2 work)

Does it interprets VAR3=$VAR1_$VAR2 as VAR3=$(VAR1_$VAR2) ?

2 Answers 2

10

Try

$ echo ${VAR1}_${VAR2}

Without the braces, it parses the combination as ${VAR1_}${VAR2}. Since you do not have a $VAR1_ variable defined, you see only the value of $VAR2.

You can see this if you define a variable $VAR1_:

$ VAR1_=another
$ echo $VAR1_$VAR2
anothertesting
Sign up to request clarification or add additional context in comments.

Comments

2

try:

VAR3="$VAR1"_"$VAR2"

it interprets VAR3=$VAR1_$VAR2 as $VAR1_ + $VAR2 --- there is no variable named $VAR1_

1 Comment

An alternative would be: VAR3=${VAR1}_${VAR2}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.