Skip to main content

Posts

Showing posts with the label dynamic

bash dynamic variable name

Q : how to concatenate variable name in linux? Q : how to compose a variable name in linux from other variables? Q : create variable name dynamically A : sometimes you need to be able to dynamically create a variable name from parts. For example, you have a set of PCs the script should run on, and the params for the script are dependent on the PC hostname. Assuming the hosts are A and B: A_PARAM1=1 A_PARAM2=2 B_PARAM1=4 B_PARAM2=5 Now the script local working var param1 would be a concatenation of <hostname>+_PARAM1 Putting it all together: !#/bin/bash host=$(hostname) param1=$(eval "echo $${host}_PARAM1") param2=$(eval "echo $${host}_PARAM2")