1

I have the following system variables:

$var01="this is var01"
$var02="this is var02"
$var03="this is var03"
$Var04="this is var04"

I want to use a for loop to recall each of them:

for i in {0..4}
do
 echo "content of var$i is: $[var$i]"
done

but the above for loop gives me an error saying "bad substitution". How should I construct echo parameter so to get the following result:

content of var01 is: this is var01
content of var02 is: this is var02
content of var03 is: this is var03
content of var04 is: this is var04
Cyrus
  • 84,225
  • 14
  • 89
  • 153
user97662
  • 942
  • 1
  • 10
  • 29

1 Answers1

1

I suggest with bash >= 4.3:

for i in {0..4}; do x="var$i"; echo "${!x}"; done

See: Nameref

Cyrus
  • 84,225
  • 14
  • 89
  • 153