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?

Peter Mortensen
  • 2,318
  • 5
  • 23
  • 24
nnachefski
  • 123
  • 4

3 Answers3

3

Replace

$NODE_ROOT{1..3}.$INFRA_DOMAIN

by

${NODE_ROOT}{1..3}.$INFRA_DOMAIN
Cyrus
  • 927
  • 1
  • 7
  • 15
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:

user9517
  • 115,471
  • 20
  • 215
  • 297
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
Khaled
  • 36,533
  • 8
  • 72
  • 99