Is it possible to build variable names from other variables in bash? [duplicate]
This question already has an answer here:
Dynamic variable names in Bash
11 answers
I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:
for I in 1 2 3 4
echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.
Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I
Is this possible in Bash?
bash variables
marked as duplicate by tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Aug 24 '16 at 8:56
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 |
This question already has an answer here:
Dynamic variable names in Bash
11 answers
I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:
for I in 1 2 3 4
echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.
Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I
Is this possible in Bash?
bash variables
marked as duplicate by tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Aug 24 '16 at 8:56
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 |
This question already has an answer here:
Dynamic variable names in Bash
11 answers
I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:
for I in 1 2 3 4
echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.
Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I
Is this possible in Bash?
bash variables
This question already has an answer here:
Dynamic variable names in Bash
11 answers
I apologise for the pretty terrible title - and the poor quality post - but what I basically want to do is this:
for I in 1 2 3 4
echo $VAR$I # echo the contents of $VAR1, $VAR2, $VAR3, etc.
Obviously the above does not work - it will (I think) try and echo the variable called $VAR$I
Is this possible in Bash?
This question already has an answer here:
Dynamic variable names in Bash
11 answers
bash variables
bash variables
asked Oct 18 '10 at 21:20
StephenStephen
4,32122851
4,32122851
marked as duplicate by tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Aug 24 '16 at 8:56
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 tripleee
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Aug 24 '16 at 8:56
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 |
7 Answers
7
active
oldest
votes
Yes, but don't do that. Use an array instead.
If you still insist on doing it that way...
$ foo1=123
$ bar=foo1
$ echo "${!bar}"
123
5
+1 for but don't do that. Use an array instead.
– Nifle
Oct 18 '10 at 21:25
Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.
– Stephen
Oct 18 '10 at 21:44
it doesnt work forkshell
– g4ur4v
Jul 23 '14 at 10:28
add a comment |
for I in 1 2 3 4 5; do
TMP="VAR$I"
echo ${!TMP}
done
I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.
add a comment |
You should think about using bash
arrays for this sort of work:
pax> set arr=(9 8 7 6)
pax> set idx=2
pax> echo ${arr[!idx]}
7
add a comment |
For the case where you don't want to refactor your variables into arrays...
One way...
$ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done
Another way...
$ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done
I haven't found a simpler way that works. For example, this does not work...
$ for I in 1 2 3 4; do echo ${!VAR$I} ; done
bash: ${!VAR$I}: bad substitution
1
Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.
– nobar
Apr 2 '14 at 16:16
add a comment |
for I in {1..5}; do
echo $((VAR$I))
done
add a comment |
Yes. See Advanced Bash-Scripting Guide
It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!
– Charles Roberto Canato
Dec 6 '17 at 19:16
add a comment |
This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays
add a comment |
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, but don't do that. Use an array instead.
If you still insist on doing it that way...
$ foo1=123
$ bar=foo1
$ echo "${!bar}"
123
5
+1 for but don't do that. Use an array instead.
– Nifle
Oct 18 '10 at 21:25
Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.
– Stephen
Oct 18 '10 at 21:44
it doesnt work forkshell
– g4ur4v
Jul 23 '14 at 10:28
add a comment |
Yes, but don't do that. Use an array instead.
If you still insist on doing it that way...
$ foo1=123
$ bar=foo1
$ echo "${!bar}"
123
5
+1 for but don't do that. Use an array instead.
– Nifle
Oct 18 '10 at 21:25
Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.
– Stephen
Oct 18 '10 at 21:44
it doesnt work forkshell
– g4ur4v
Jul 23 '14 at 10:28
add a comment |
Yes, but don't do that. Use an array instead.
If you still insist on doing it that way...
$ foo1=123
$ bar=foo1
$ echo "${!bar}"
123
Yes, but don't do that. Use an array instead.
If you still insist on doing it that way...
$ foo1=123
$ bar=foo1
$ echo "${!bar}"
123
answered Oct 18 '10 at 21:22
Ignacio Vazquez-AbramsIgnacio Vazquez-Abrams
591k10710791183
591k10710791183
5
+1 for but don't do that. Use an array instead.
– Nifle
Oct 18 '10 at 21:25
Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.
– Stephen
Oct 18 '10 at 21:44
it doesnt work forkshell
– g4ur4v
Jul 23 '14 at 10:28
add a comment |
5
+1 for but don't do that. Use an array instead.
– Nifle
Oct 18 '10 at 21:25
Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.
– Stephen
Oct 18 '10 at 21:44
it doesnt work forkshell
– g4ur4v
Jul 23 '14 at 10:28
5
5
+1 for but don't do that. Use an array instead.
– Nifle
Oct 18 '10 at 21:25
+1 for but don't do that. Use an array instead.
– Nifle
Oct 18 '10 at 21:25
Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.
– Stephen
Oct 18 '10 at 21:44
Thanks for the advice re arrays. Using your and pax's post, I managed to get it working. Cheers guys.
– Stephen
Oct 18 '10 at 21:44
it doesnt work for
kshell
– g4ur4v
Jul 23 '14 at 10:28
it doesnt work for
kshell
– g4ur4v
Jul 23 '14 at 10:28
add a comment |
for I in 1 2 3 4 5; do
TMP="VAR$I"
echo ${!TMP}
done
I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.
add a comment |
for I in 1 2 3 4 5; do
TMP="VAR$I"
echo ${!TMP}
done
I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.
add a comment |
for I in 1 2 3 4 5; do
TMP="VAR$I"
echo ${!TMP}
done
I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.
for I in 1 2 3 4 5; do
TMP="VAR$I"
echo ${!TMP}
done
I have a general rule that if I need indirect access to variables (ditto arrays), then it is time to convert the script from shell into Perl/Python/etc. Advanced coding in shell though possible quickly becomes a mess.
edited Oct 18 '10 at 21:37
answered Oct 18 '10 at 21:27
Dummy00001Dummy00001
12.5k22454
12.5k22454
add a comment |
add a comment |
You should think about using bash
arrays for this sort of work:
pax> set arr=(9 8 7 6)
pax> set idx=2
pax> echo ${arr[!idx]}
7
add a comment |
You should think about using bash
arrays for this sort of work:
pax> set arr=(9 8 7 6)
pax> set idx=2
pax> echo ${arr[!idx]}
7
add a comment |
You should think about using bash
arrays for this sort of work:
pax> set arr=(9 8 7 6)
pax> set idx=2
pax> echo ${arr[!idx]}
7
You should think about using bash
arrays for this sort of work:
pax> set arr=(9 8 7 6)
pax> set idx=2
pax> echo ${arr[!idx]}
7
answered Oct 18 '10 at 21:25
paxdiablopaxdiablo
643k17512671688
643k17512671688
add a comment |
add a comment |
For the case where you don't want to refactor your variables into arrays...
One way...
$ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done
Another way...
$ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done
I haven't found a simpler way that works. For example, this does not work...
$ for I in 1 2 3 4; do echo ${!VAR$I} ; done
bash: ${!VAR$I}: bad substitution
1
Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.
– nobar
Apr 2 '14 at 16:16
add a comment |
For the case where you don't want to refactor your variables into arrays...
One way...
$ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done
Another way...
$ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done
I haven't found a simpler way that works. For example, this does not work...
$ for I in 1 2 3 4; do echo ${!VAR$I} ; done
bash: ${!VAR$I}: bad substitution
1
Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.
– nobar
Apr 2 '14 at 16:16
add a comment |
For the case where you don't want to refactor your variables into arrays...
One way...
$ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done
Another way...
$ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done
I haven't found a simpler way that works. For example, this does not work...
$ for I in 1 2 3 4; do echo ${!VAR$I} ; done
bash: ${!VAR$I}: bad substitution
For the case where you don't want to refactor your variables into arrays...
One way...
$ for I in 1 2 3 4; do TEMP=VAR$I ; echo ${!TEMP} ; done
Another way...
$ for I in 1 2 3 4; do eval echo $$(eval echo VAR$I) ; done
I haven't found a simpler way that works. For example, this does not work...
$ for I in 1 2 3 4; do echo ${!VAR$I} ; done
bash: ${!VAR$I}: bad substitution
answered Apr 2 '14 at 16:14
nobarnobar
27.1k1087100
27.1k1087100
1
Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.
– nobar
Apr 2 '14 at 16:16
add a comment |
1
Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.
– nobar
Apr 2 '14 at 16:16
1
1
Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.
– nobar
Apr 2 '14 at 16:16
Part of my answer was previously posted by @Dummy00001 -- the only person who had actually answered the question as asked.
– nobar
Apr 2 '14 at 16:16
add a comment |
for I in {1..5}; do
echo $((VAR$I))
done
add a comment |
for I in {1..5}; do
echo $((VAR$I))
done
add a comment |
for I in {1..5}; do
echo $((VAR$I))
done
for I in {1..5}; do
echo $((VAR$I))
done
answered Apr 20 '16 at 20:42
Ahmed MokhtarAhmed Mokhtar
496
496
add a comment |
add a comment |
Yes. See Advanced Bash-Scripting Guide
It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!
– Charles Roberto Canato
Dec 6 '17 at 19:16
add a comment |
Yes. See Advanced Bash-Scripting Guide
It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!
– Charles Roberto Canato
Dec 6 '17 at 19:16
add a comment |
Yes. See Advanced Bash-Scripting Guide
Yes. See Advanced Bash-Scripting Guide
answered Aug 17 '15 at 13:34
gerardwgerardw
3,1202228
3,1202228
It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!
– Charles Roberto Canato
Dec 6 '17 at 19:16
add a comment |
It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!
– Charles Roberto Canato
Dec 6 '17 at 19:16
It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!
– Charles Roberto Canato
Dec 6 '17 at 19:16
It would have been nice to transcribe some part of the linked page for direct understanding in your answer... but such a great reference!
– Charles Roberto Canato
Dec 6 '17 at 19:16
add a comment |
This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays
add a comment |
This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays
add a comment |
This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays
This definately looks like an array type of situation. Here's a link that has a very nice discussion (with many examples) of how to use arrays in bash: Arrays
answered Oct 18 '10 at 21:26
AnthonyAnthony
5,52573568
5,52573568
add a comment |
add a comment |