Bash - expanding variables names within variable names
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
add a comment |
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
Bash supports array variables. If your indexx
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
add a comment |
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
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
bash environment-variables
asked Jan 25 at 18:19
VolumetricsteveVolumetricsteve
180412
180412
Bash supports array variables. If your indexx
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
add a comment |
Bash supports array variables. If your indexx
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
add a comment |
1 Answer
1
active
oldest
votes
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
Thank you so......so much.
– Volumetricsteve
Jan 25 at 18:40
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
Thank you so......so much.
– Volumetricsteve
Jan 25 at 18:40
add a comment |
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
Thank you so......so much.
– Volumetricsteve
Jan 25 at 18:40
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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