jay
jay

Reputation: 65

Bash and variables

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) ?

Upvotes: 2

Views: 204

Answers (2)

James.Xu
James.Xu

Reputation: 8295

try:

VAR3="$VAR1"_"$VAR2"

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

Upvotes: 2

user729124
user729124

Reputation:

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

Upvotes: 10

Related Questions