Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to use a variable in a variable with a for loop bash script?

Is it possible to create a variable using a varible? like the script below?

Any help will be apprciated.

#!/bin/bash
for i in {1..10}
do
dog${i}=test${i}
echo $dog${i}
done

making the varible outside of the for loop works but doesnt inside, is there a work around?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have tried many different uses of brackets and parenthesis but to no success

>Solution :

#!/bin/bash
for i in {1..10}
do
  test="dog$i"
  val="test$i"
  declare "$test=$val"
  echo "${!test}"
done
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading