6

I think an example is in order. Consider I have these two environment variables setup (by a Continuous Delivery tool): PACKAGE_VER=1.2.3 and USE_VERSION_FROM=PACKAGE_VER.

What I want now is a script that can't predict the name of the variable pointing out 1.2.3, only USE_VERSION_FROM. And so, I'm looking for a way to get my script to know the version to use (1.2.3) without knowing anything other than that USE_VERSION_FROM points out the name of the variable that holds the version number.

My naïve non-working first attempt:

#!/bin/bash
VERSION_TO_USE="${${USE_VERSION_FROM}}"
6
PACKAGE_VER=1.2.3
USE_VERSION_FROM=PACKAGE_VER
VERSION_TO_USE=${!USE_VERSION_FROM}
echo $VERSION_TO_USE
1.2.3

This is "indirect parameter expansion" as described in the bash manual

Note, it's not strictly necessary to provide quotes on a variable assignment a=$b vs a="$b" -- word splitting is not performed during an assignment (can't find the reference for that just now)

| improve this answer | |

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.