2

Why doesn't this work?

NODE_ROOT=node0
INFRA_DOMAIN=example.com
for host in $NODE_ROOT{1..3}.$INFRA_DOMAIN; do echo $host; done

I'm expecting:

node01.example.com
node02.example.com
node03.example.com

However, I get:

.example.com
.example.com
.example.com

How do I do this properly?

3

Replace

$NODE_ROOT{1..3}.$INFRA_DOMAIN

by

${NODE_ROOT}{1..3}.$INFRA_DOMAIN
0
3

The issue with your script is that Bash is not able to interpret $NODE_ROOT{1..3} correctly. To help, you need to enclose the variable in quotes, ", which is generally accepted as 'good practice' in any case. You can also use the "${VARIABLE}"-type syntax to help Bash out too.

For example,

NODE_ROOT=node0
INFRA_DOMAIN=example.com
for host in "$NODE_ROOT"{1..3}."$INFRA_DOMAIN"; do echo $host; done

or

for host in "${NODE_ROOT}"{1..3}."$INFRA_DOMAIN"; do echo $host; done

Further reading:

0

You can simply modify your script to read:

NODE_ROOT=node0
INFRA_DOMAIN=example.com
for n in {1..3}; do echo ${NODE_ROOT}${n}.${INFRA_DOMAIN}; done

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.