variable with a variable substring in bash [duplicate]

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
This question already has an answer here:
How to do indirect variable evaluation
5 answers
In bash, I have defined
chg_Li=3
chg_Na=9
Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.
What is the correct syntax to call $chg_$i?
bash variable
marked as duplicate by don_crissti, ñÃÂsýù÷, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
This question already has an answer here:
How to do indirect variable evaluation
5 answers
In bash, I have defined
chg_Li=3
chg_Na=9
Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.
What is the correct syntax to call $chg_$i?
bash variable
marked as duplicate by don_crissti, ñÃÂsýù÷, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
This question already has an answer here:
How to do indirect variable evaluation
5 answers
In bash, I have defined
chg_Li=3
chg_Na=9
Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.
What is the correct syntax to call $chg_$i?
bash variable
This question already has an answer here:
How to do indirect variable evaluation
5 answers
In bash, I have defined
chg_Li=3
chg_Na=9
Now I want to call $chg_$i where i is in a for loop, looping over Li and Na.
What is the correct syntax to call $chg_$i?
This question already has an answer here:
How to do indirect variable evaluation
5 answers
bash variable
bash variable
edited Oct 3 '17 at 22:10
ñÃÂsýù÷
15.6k92563
15.6k92563
asked Oct 3 '17 at 22:05
Lei Zhang
1
1
marked as duplicate by don_crissti, ñÃÂsýù÷, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by don_crissti, ñÃÂsýù÷, Jeff Schaller, peterh, jasonwryan Oct 3 '17 at 23:05
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
1
down vote
You can use eval or indirection:
eval echo $chg_$i
or
varname=$chg_$i
echo $!varname
1
Preferably indirection. When usingeval, your first thought should be "If I'm usingeval, I probably need to rethink my approach" in nearly all cases.
â DopeGhoti
Oct 3 '17 at 22:34
add a comment |Â
up vote
0
down vote
As was mentioned, you can use indirection for this:
$ chg_Li=3
$ chg_Na=9
$ post_fix=(Li Na)
$ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
3
9
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can use eval or indirection:
eval echo $chg_$i
or
varname=$chg_$i
echo $!varname
1
Preferably indirection. When usingeval, your first thought should be "If I'm usingeval, I probably need to rethink my approach" in nearly all cases.
â DopeGhoti
Oct 3 '17 at 22:34
add a comment |Â
up vote
1
down vote
You can use eval or indirection:
eval echo $chg_$i
or
varname=$chg_$i
echo $!varname
1
Preferably indirection. When usingeval, your first thought should be "If I'm usingeval, I probably need to rethink my approach" in nearly all cases.
â DopeGhoti
Oct 3 '17 at 22:34
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You can use eval or indirection:
eval echo $chg_$i
or
varname=$chg_$i
echo $!varname
You can use eval or indirection:
eval echo $chg_$i
or
varname=$chg_$i
echo $!varname
answered Oct 3 '17 at 22:07
Hauke Laging
53.7k1282130
53.7k1282130
1
Preferably indirection. When usingeval, your first thought should be "If I'm usingeval, I probably need to rethink my approach" in nearly all cases.
â DopeGhoti
Oct 3 '17 at 22:34
add a comment |Â
1
Preferably indirection. When usingeval, your first thought should be "If I'm usingeval, I probably need to rethink my approach" in nearly all cases.
â DopeGhoti
Oct 3 '17 at 22:34
1
1
Preferably indirection. When using
eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.â DopeGhoti
Oct 3 '17 at 22:34
Preferably indirection. When using
eval, your first thought should be "If I'm using eval, I probably need to rethink my approach" in nearly all cases.â DopeGhoti
Oct 3 '17 at 22:34
add a comment |Â
up vote
0
down vote
As was mentioned, you can use indirection for this:
$ chg_Li=3
$ chg_Na=9
$ post_fix=(Li Na)
$ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
3
9
add a comment |Â
up vote
0
down vote
As was mentioned, you can use indirection for this:
$ chg_Li=3
$ chg_Na=9
$ post_fix=(Li Na)
$ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
3
9
add a comment |Â
up vote
0
down vote
up vote
0
down vote
As was mentioned, you can use indirection for this:
$ chg_Li=3
$ chg_Na=9
$ post_fix=(Li Na)
$ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
3
9
As was mentioned, you can use indirection for this:
$ chg_Li=3
$ chg_Na=9
$ post_fix=(Li Na)
$ for j in $post_fix[@]; do chg="chg_$j"; echo "$!chg"; done;
3
9
answered Oct 3 '17 at 22:50
FloHe
63728
63728
add a comment |Â
add a comment |Â