I have a properties file to set some environment variables say:
mydata.properties:
VAR1=Data1
VAR2=Data2
Now I want to define a new variable called VAR3 that can hold either VAR1 or VAR2 like:
mydata.properties:
VAR1=Data1
VAR2=Data2
VAR3=VAR2
To make these variables to be available to my bash session I am using this command
source mydata.properties
Now my requirement is to print the value for VAR3 so that it can print the internal data of the sourced VAR2 variable like:
Value of VAR3 is Data2
I tried different options like
echo "Value of VAR3 is $$VAR3"
This gives me junk output.
or echo "Value of VAR3 is ${$VAR3}"
This give me error as Value of ${$VAR3}: bad substitution
Please help me how to get this output.
If you really need to expand the variable that VAR3 points to (instead of just setting VAR3 to the value of VAR2 to begin with), you can use indirect variable expansion with ${!varname}
:
$ VAR1=Data1
$ VAR2=Data2
$ VAR3=VAR2
$ echo "${!VAR3}"
Data2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With