Bash - expanding variables names within variable names












0















In a simple for loop, I'm trying to get it to increment which variable it echos. The unrolled version at the bottom works and does what I want, but how can I get the loop to work the same way?



for x in 0 1 2 3 4 do 
echo -ne $FVAR$x ":: "
echo $LVAR$x
done





echo -ne $FVAR0 ":: "
echo $LVAR0
echo -ne $FVAR1 ":: "
echo $LVAR1
echo -ne $FVAR2 ":: "
echo $LVAR2
echo -ne $FVAR3 ":: "
echo $LVAR3
echo -ne $FVAR4 ":: "
echo $LVAR4









share|improve this question























  • Bash supports array variables. If your index x is a simple integer, that may be something you could consider, referencing them as ${FVAR[0]} or ${LVAR[4]}, etc.

    – Jim L.
    Jan 25 at 18:32











  • Given this context, that's fair. The only trouble is the code I'm supporting uses variables in this way because earlier on, exporting must be done, so arrays aren't an option. I should have specified that in the question, but I am hoping to find some way to just have echo see that $FVAR$x should mean $FVAR0 on the first iteration of the loop.

    – Volumetricsteve
    Jan 25 at 18:35


















0















In a simple for loop, I'm trying to get it to increment which variable it echos. The unrolled version at the bottom works and does what I want, but how can I get the loop to work the same way?



for x in 0 1 2 3 4 do 
echo -ne $FVAR$x ":: "
echo $LVAR$x
done





echo -ne $FVAR0 ":: "
echo $LVAR0
echo -ne $FVAR1 ":: "
echo $LVAR1
echo -ne $FVAR2 ":: "
echo $LVAR2
echo -ne $FVAR3 ":: "
echo $LVAR3
echo -ne $FVAR4 ":: "
echo $LVAR4









share|improve this question























  • Bash supports array variables. If your index x is a simple integer, that may be something you could consider, referencing them as ${FVAR[0]} or ${LVAR[4]}, etc.

    – Jim L.
    Jan 25 at 18:32











  • Given this context, that's fair. The only trouble is the code I'm supporting uses variables in this way because earlier on, exporting must be done, so arrays aren't an option. I should have specified that in the question, but I am hoping to find some way to just have echo see that $FVAR$x should mean $FVAR0 on the first iteration of the loop.

    – Volumetricsteve
    Jan 25 at 18:35
















0












0








0








In a simple for loop, I'm trying to get it to increment which variable it echos. The unrolled version at the bottom works and does what I want, but how can I get the loop to work the same way?



for x in 0 1 2 3 4 do 
echo -ne $FVAR$x ":: "
echo $LVAR$x
done





echo -ne $FVAR0 ":: "
echo $LVAR0
echo -ne $FVAR1 ":: "
echo $LVAR1
echo -ne $FVAR2 ":: "
echo $LVAR2
echo -ne $FVAR3 ":: "
echo $LVAR3
echo -ne $FVAR4 ":: "
echo $LVAR4









share|improve this question














In a simple for loop, I'm trying to get it to increment which variable it echos. The unrolled version at the bottom works and does what I want, but how can I get the loop to work the same way?



for x in 0 1 2 3 4 do 
echo -ne $FVAR$x ":: "
echo $LVAR$x
done





echo -ne $FVAR0 ":: "
echo $LVAR0
echo -ne $FVAR1 ":: "
echo $LVAR1
echo -ne $FVAR2 ":: "
echo $LVAR2
echo -ne $FVAR3 ":: "
echo $LVAR3
echo -ne $FVAR4 ":: "
echo $LVAR4






bash environment-variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 25 at 18:19









VolumetricsteveVolumetricsteve

180412




180412













  • Bash supports array variables. If your index x is a simple integer, that may be something you could consider, referencing them as ${FVAR[0]} or ${LVAR[4]}, etc.

    – Jim L.
    Jan 25 at 18:32











  • Given this context, that's fair. The only trouble is the code I'm supporting uses variables in this way because earlier on, exporting must be done, so arrays aren't an option. I should have specified that in the question, but I am hoping to find some way to just have echo see that $FVAR$x should mean $FVAR0 on the first iteration of the loop.

    – Volumetricsteve
    Jan 25 at 18:35





















  • Bash supports array variables. If your index x is a simple integer, that may be something you could consider, referencing them as ${FVAR[0]} or ${LVAR[4]}, etc.

    – Jim L.
    Jan 25 at 18:32











  • Given this context, that's fair. The only trouble is the code I'm supporting uses variables in this way because earlier on, exporting must be done, so arrays aren't an option. I should have specified that in the question, but I am hoping to find some way to just have echo see that $FVAR$x should mean $FVAR0 on the first iteration of the loop.

    – Volumetricsteve
    Jan 25 at 18:35



















Bash supports array variables. If your index x is a simple integer, that may be something you could consider, referencing them as ${FVAR[0]} or ${LVAR[4]}, etc.

– Jim L.
Jan 25 at 18:32





Bash supports array variables. If your index x is a simple integer, that may be something you could consider, referencing them as ${FVAR[0]} or ${LVAR[4]}, etc.

– Jim L.
Jan 25 at 18:32













Given this context, that's fair. The only trouble is the code I'm supporting uses variables in this way because earlier on, exporting must be done, so arrays aren't an option. I should have specified that in the question, but I am hoping to find some way to just have echo see that $FVAR$x should mean $FVAR0 on the first iteration of the loop.

– Volumetricsteve
Jan 25 at 18:35







Given this context, that's fair. The only trouble is the code I'm supporting uses variables in this way because earlier on, exporting must be done, so arrays aren't an option. I should have specified that in the question, but I am hoping to find some way to just have echo see that $FVAR$x should mean $FVAR0 on the first iteration of the loop.

– Volumetricsteve
Jan 25 at 18:35












1 Answer
1






active

oldest

votes


















1














From the bash manual:-




If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable
indirection. Bash uses the
value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is
used in the rest of the
substitution, rather than the value of parameter itself. This is known as indirect expansion.




So you need to store the name of the variable you want to expand in a separate variable, eg:-



for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo ${!fv} ":: " ${!lv}
done


You could alternatively define fv and lv as of type nameref: the code would be similar, except that there is no need for ! to expand the variables:-



declare -n fv
declare -n lv

for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo $fv ":: " $lv
done





share|improve this answer


























  • Thank you so......so much.

    – Volumetricsteve
    Jan 25 at 18:40











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398506%2fbash-expanding-variables-names-within-variable-names%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














From the bash manual:-




If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable
indirection. Bash uses the
value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is
used in the rest of the
substitution, rather than the value of parameter itself. This is known as indirect expansion.




So you need to store the name of the variable you want to expand in a separate variable, eg:-



for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo ${!fv} ":: " ${!lv}
done


You could alternatively define fv and lv as of type nameref: the code would be similar, except that there is no need for ! to expand the variables:-



declare -n fv
declare -n lv

for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo $fv ":: " $lv
done





share|improve this answer


























  • Thank you so......so much.

    – Volumetricsteve
    Jan 25 at 18:40
















1














From the bash manual:-




If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable
indirection. Bash uses the
value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is
used in the rest of the
substitution, rather than the value of parameter itself. This is known as indirect expansion.




So you need to store the name of the variable you want to expand in a separate variable, eg:-



for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo ${!fv} ":: " ${!lv}
done


You could alternatively define fv and lv as of type nameref: the code would be similar, except that there is no need for ! to expand the variables:-



declare -n fv
declare -n lv

for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo $fv ":: " $lv
done





share|improve this answer


























  • Thank you so......so much.

    – Volumetricsteve
    Jan 25 at 18:40














1












1








1







From the bash manual:-




If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable
indirection. Bash uses the
value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is
used in the rest of the
substitution, rather than the value of parameter itself. This is known as indirect expansion.




So you need to store the name of the variable you want to expand in a separate variable, eg:-



for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo ${!fv} ":: " ${!lv}
done


You could alternatively define fv and lv as of type nameref: the code would be similar, except that there is no need for ! to expand the variables:-



declare -n fv
declare -n lv

for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo $fv ":: " $lv
done





share|improve this answer















From the bash manual:-




If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of variable
indirection. Bash uses the
value of the variable formed from the rest of parameter as the name of the variable; this variable is then expanded and that value is
used in the rest of the
substitution, rather than the value of parameter itself. This is known as indirect expansion.




So you need to store the name of the variable you want to expand in a separate variable, eg:-



for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo ${!fv} ":: " ${!lv}
done


You could alternatively define fv and lv as of type nameref: the code would be similar, except that there is no need for ! to expand the variables:-



declare -n fv
declare -n lv

for x in 0 1 2 3 4
do fv=FVAR$x
lv=LVAR$x
echo $fv ":: " $lv
done






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 25 at 18:50

























answered Jan 25 at 18:38









AFHAFH

14.3k31938




14.3k31938













  • Thank you so......so much.

    – Volumetricsteve
    Jan 25 at 18:40



















  • Thank you so......so much.

    – Volumetricsteve
    Jan 25 at 18:40

















Thank you so......so much.

– Volumetricsteve
Jan 25 at 18:40





Thank you so......so much.

– Volumetricsteve
Jan 25 at 18:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1398506%2fbash-expanding-variables-names-within-variable-names%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

Mangá

Eduardo VII do Reino Unido