Dynamically Construct Variables Inside the Script and Fetch Values from Source Parameters

Dynamically Construct Variables Inside the Script and Fetch Values from Source Parameters

I have a txt file extract.dat which is read inside my code using source.

$ cat extract.dat
ExchgRate_prop="EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate"
Compliance_prop="EDB_NAME=share_compliance SRC_WDS=wca PN=com"
Unitcost_prop=="EDB_NAME=share_unitcost SRC_WDS=wda PN=unit"

source ./ extract.dat

now I would have got all the varaibles inside my code. if my code takes an argument $PROJ with value as 'ExchgRate' , how can i concatenate "$PROJ""_prop" to create the variable ExchgRate_prop and then get value "EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate"

1 Answer

You can use bash variable indirection. Ex. given

$ echo "$ExchgRate_prop" 
EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate

and

$ PROJ=ExchgRate

then

$ var=${PROJ}_prop
$ echo "${!var}"
EDB_NAME=share_exchange SRC_WDS=wds PN=ExchgRate
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.