0

I have set the following Unix environment variables.

export Dev_Branch=temp
export QA_Branch=stage
export Prod_Branch=master

Now I am taking the environment name as input to my script and I need to get the corresponding branch name. So, if user inputs "Dev" it should fetch "temp", "QA" should get me "stage" and "Prod" should get me "master".

The code I am using is

br_name=`echo \$"${1}"_Branch`

But br_name shows something like $Dev_Branch instead of giving the actual branch name.

1 Answer 1

3

What you want is variable indirection using nameref:

$ export QA_Branch=stage
$ what="QA"

# ksh indirection
$ nameref br_name="${what}_Branch"
$ echo "$br_name"
stage

# dash indirection using eval, also works in ksh, bash
$ eval "br_name=\$${what}_Branch"
$ echo "$br_name"
stage

# bash indirection
$ br_name="$what"_Branch
$ echo "${!br_name}"
stage

And please use lowercase or uppercase variables. The exported variables should be uppercase and local variables lowercase.

0

You must log in to answer this question.