1

We need to get the value of dynamically constructed variables.

What I mean is we have a variable loaded from a property file called data8967677878788node. So when we run echo $data8967677878788node we get the output test.

Now in data8967677878788node the number part 8967677878788 needs to be dynamic. That means there could be variables like

data1234node
data346346367node

and such.

The number is an input argument to the script. So we need something like this to work

TESTVAR="data`echo $DATANUMBER`node"
echo $$TESTVAR #This line gives the value "test"

Any idea on how this can be accomplished

2 Answers 2

4

You can use BASH's indirect variable expansion:

data346346367node='test'

myfunc() {
    datanumber="$1"
    var1="data${datanumber}node"
    echo "${!var1}"
}

And call it as:

myfunc 346346367

Output:

test
Sign up to request clarification or add additional context in comments.

Comments

0

Your code is actually already pretty close to working, it just needs to be modified slightly:

TESTVAR="data`echo $DATANUMBER`node"
echo ${!TESTVAR}

If $DATANUMBER has the value 12345 and $data12345node has the value test then the above snippet will output test.

Source: http://wiki.bash-hackers.org/syntax/pe#indirection

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.