loops - Bash: not evaluating variable correctly -


i have simple bash function includes loop capture hostnames x number of times (defined in $number_of_hosts):

number_of_hosts="3"  in $(seq 1 $number_of_hosts);      read -ep "enter hostname" host$i done 

if echo out each variable individually work. example

echo $host1 echo $host2 echo $host3 

however, if try add loop around (used in later function), can't seem work. using

for in $(seq 1 $number_of_hosts);      echo $host$i done 

how can echo values captured 'read' earlier?

thank in advance help.

there few grimy ways use "variable variables" wouldn't recommend them. why not use array?

#!/bin/bash  number_of_hosts=3 in $(seq 1 "$number_of_hosts");      read -ep "enter hostname" 'host[i]' done  echo "${host[1]}" echo "${host[2]}" echo "${host[3]}" 

if want avoid calling seq, can change loop:

for (( = 1; <= number_of_hosts; ++i ));     read -ep "enter hostname" 'host[i]' done 

Comments