0

I have following shell script -

AWSZONE="central"
ENVIRONMENT="staging"
REMOTE_HOSTS_central_staging="172.31.7.59,172.31.3.151
NAME="REMOTE_HOSTS_${AWSZONE}_${ENVIRONMENT}"
echo "Using following agents: ${NAME}"

When executing I get following output -

Using following agents: REMOTE_HOSTS_central_staging

Though I would like to get output -

Using following agents: 172.31.7.59,172.31.3.151

What is wrong with my syntax?

Tarun
  • 3,456
  • 10
  • 48
  • 82
  • eval "NAME=\$REMOTE_HOSTS_${AWSZONE}_${ENVIRONMENT}" – Chris Mar 30 '16 at 09:26
  • Possible duplicate of [how to use a variable's value as other variable's name in bash](http://stackoverflow.com/questions/9714902/how-to-use-a-variables-value-as-other-variables-name-in-bash) – Julien Lopez Mar 30 '16 at 09:37

2 Answers2

2

Change the echo to

echo "Using following agents: ${!NAME}"

And it should be OK.

LarsErik
  • 138
  • 1
  • 7
1

Try this:

AWSZONE="central"
ENVIRONMENT="staging"
REMOTE_HOSTS_central_staging="172.31.7.59,172.31.3.151"
NAME="REMOTE_HOSTS_${AWSZONE}_${ENVIRONMENT}"
eval "echo Using following agents: \$${NAME}"

Output:

Using following agents: 172.31.7.59,172.31.3.151

If you use bash, you can also use @LarsErik's answer.It is known as Indirect Expansion

Ren
  • 2,852
  • 2
  • 23
  • 45