Alain
Alain

Reputation: 722

Bash access property loaded from a properties files with a variable

let's say we have a properties file: myprops.properties
with the following key pair values:

In my script I wrote these lines:

_ENVIRONMENT="test" 
. myprops.properties # load the properties

I would like to access the user property starting with $_ENVIRONMENT i.e."test"

_USER=$_ENVIRONMENT"_user" # this does not work it outputs "testuser" which is the key and not the value.

If I declare:

echo ${testuser} # that works I get the value

What I am missing? Is it possible?

Thanks in advance,

Upvotes: 1

Views: 43

Answers (1)

anubhava
anubhava

Reputation: 785611

You can use indirect variable reference:

varUser="${_ENVIRONMENT}_user"
echo "${!varUser}"
myuser

Upvotes: 2

Related Questions