I have the following script stored in test.sh:
#!/bin/sh
key1=foo
key2=bar
key3=baz
for i in 1 2 3; do
echo $i
echo ${key${i}}
done
When I run it I get the output
1
test.sh: 9: Bad substitution
What am I doing wrong?
I have the following script stored in test.sh:
#!/bin/sh
key1=foo
key2=bar
key3=baz
for i in 1 2 3; do
echo $i
echo ${key${i}}
done
When I run it I get the output
1
test.sh: 9: Bad substitution
What am I doing wrong?
Is this what you are looking for?
#!/bin/sh
key1=foo
key2=bar
key3=baz
for i in 1 2 3; do
echo $i
eval echo \$"key$i"
done
Output:
1
foo
2
bar
3
baz
There is an answer to this question here: https://stackoverflow.com/a/917313/10693596
Short answer is this is not possible in bash
.
Update: depending on what you are ultimately after, using indirect expansion might work: https://stackoverflow.com/a/42066820/10693596