Resolve Variable in a Variable in shell
I have to use environment vars in my bash
shell and:
echo $ENV
resolves toDEV
,STAGE
orPRD
depending upon the stack.
echo $DEV_ACCOUNT
resolves to a number e.g.echo $DEV_ACCOUNT
gives12345678
I need to use echo $ENV_ACCOUNT
in a command which should resolve to 12345678
but it is resolving to DEV_ACCOUNT
instead of 12345678
.
The command I am using:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-world"
The $ENV_ACCOUNT
is resolving to DEV_ACCOUNT
instead of 12345678
.
The expected output is:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:12345678:function:hello-world"
What am I doing wrong?
bash
New contributor
add a comment |
I have to use environment vars in my bash
shell and:
echo $ENV
resolves toDEV
,STAGE
orPRD
depending upon the stack.
echo $DEV_ACCOUNT
resolves to a number e.g.echo $DEV_ACCOUNT
gives12345678
I need to use echo $ENV_ACCOUNT
in a command which should resolve to 12345678
but it is resolving to DEV_ACCOUNT
instead of 12345678
.
The command I am using:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-world"
The $ENV_ACCOUNT
is resolving to DEV_ACCOUNT
instead of 12345678
.
The expected output is:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:12345678:function:hello-world"
What am I doing wrong?
bash
New contributor
2
Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
Jan 3 at 8:02
And where (and to what) are you settingENV_ACCOUNT
?
– tink
Jan 3 at 8:03
@P_Yadav aws events put-targets --rule {{ stack_name | lower }}-hello-world --targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-wolrd" This is the command where i want to resolve the account number.
– Bharat
Jan 3 at 8:09
@Bharat please edit the question.
– P_Yadav
Jan 3 at 8:09
add a comment |
I have to use environment vars in my bash
shell and:
echo $ENV
resolves toDEV
,STAGE
orPRD
depending upon the stack.
echo $DEV_ACCOUNT
resolves to a number e.g.echo $DEV_ACCOUNT
gives12345678
I need to use echo $ENV_ACCOUNT
in a command which should resolve to 12345678
but it is resolving to DEV_ACCOUNT
instead of 12345678
.
The command I am using:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-world"
The $ENV_ACCOUNT
is resolving to DEV_ACCOUNT
instead of 12345678
.
The expected output is:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:12345678:function:hello-world"
What am I doing wrong?
bash
New contributor
I have to use environment vars in my bash
shell and:
echo $ENV
resolves toDEV
,STAGE
orPRD
depending upon the stack.
echo $DEV_ACCOUNT
resolves to a number e.g.echo $DEV_ACCOUNT
gives12345678
I need to use echo $ENV_ACCOUNT
in a command which should resolve to 12345678
but it is resolving to DEV_ACCOUNT
instead of 12345678
.
The command I am using:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-world"
The $ENV_ACCOUNT
is resolving to DEV_ACCOUNT
instead of 12345678
.
The expected output is:
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:12345678:function:hello-world"
What am I doing wrong?
bash
bash
New contributor
New contributor
edited Jan 3 at 8:57
nohillside
2,382919
2,382919
New contributor
asked Jan 3 at 7:56
BharatBharat
143
143
New contributor
New contributor
2
Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
Jan 3 at 8:02
And where (and to what) are you settingENV_ACCOUNT
?
– tink
Jan 3 at 8:03
@P_Yadav aws events put-targets --rule {{ stack_name | lower }}-hello-world --targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-wolrd" This is the command where i want to resolve the account number.
– Bharat
Jan 3 at 8:09
@Bharat please edit the question.
– P_Yadav
Jan 3 at 8:09
add a comment |
2
Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
Jan 3 at 8:02
And where (and to what) are you settingENV_ACCOUNT
?
– tink
Jan 3 at 8:03
@P_Yadav aws events put-targets --rule {{ stack_name | lower }}-hello-world --targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-wolrd" This is the command where i want to resolve the account number.
– Bharat
Jan 3 at 8:09
@Bharat please edit the question.
– P_Yadav
Jan 3 at 8:09
2
2
Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
Jan 3 at 8:02
Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
Jan 3 at 8:02
And where (and to what) are you setting
ENV_ACCOUNT
?– tink
Jan 3 at 8:03
And where (and to what) are you setting
ENV_ACCOUNT
?– tink
Jan 3 at 8:03
@P_Yadav aws events put-targets --rule {{ stack_name | lower }}-hello-world --targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-wolrd" This is the command where i want to resolve the account number.
– Bharat
Jan 3 at 8:09
@P_Yadav aws events put-targets --rule {{ stack_name | lower }}-hello-world --targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-wolrd" This is the command where i want to resolve the account number.
– Bharat
Jan 3 at 8:09
@Bharat please edit the question.
– P_Yadav
Jan 3 at 8:09
@Bharat please edit the question.
– P_Yadav
Jan 3 at 8:09
add a comment |
3 Answers
3
active
oldest
votes
Note that uppercase variable names are not generally safe to use. $ENV
, for example, is special in many shells. In these shells, its value should be the pathname of a file that will be sourced before starting certain types of shells. The bash
shell uses $ENV
in POSIX mode.
So, in bash
:
declare -A account
account["develop"]=12345678
account["staging"]=9128312
account["production"]=123123
mode=$run_mode
aws events put-targets
--rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${account[$mode]}:function:hello-wolrd"
This sets up an associative array, account
, that holds the data for development, staging and production modes. It then defines the mode that we're using and executes the command with the correct data from the account
array. The run_mode
variable is assumed to be an environment variable containing one of the strings develop
, staging
or production
.
Associative arrays were introduced in bash
4.0.
I have used your aws
command as you wrote it (except for adding newlines and my account
variable), even though the {{ stack_name | lower }}-hello-world
probably does not de what you'd expect it to (it would try to execute lower
as a command). Since I don't know aws
, I don't know what it should look like, but it should probably be quoted.
Addressing user mosvy's concerns in comments: In the case where the variables DEV_ACCOUNT
etc. are environment variables and the correct one has to be picked based on what the ENV
variable is:
declare -A account
account["DEV"]=$DEV_ACCOUNT
account["STAGE"]=$STAGE_ACCOUNT
account["PRD"]=$PRD_ACCOUNT
mode=$ENV
# the rest as before
Thanks for telling about arrays. Is there any other way to resolve the variable ? I tried the arrays using vars, but not successful. Instead of strings, declare -A account account["$ENV"]="$ENV_ACCOUNT" Didn't worked. Any suggestions?
– Bharat
Jan 3 at 8:45
@Bharat I don't think I ever used a variable while setting the values in theaccount
array in my code. Theaccount
array is an array of static values for the three typesdevelop
,staging
andproduction
. Themode
variable then controls which one of these that will be used.
– Kusalananda
Jan 3 at 9:11
@Bharat don't ignore the advice about not using CAPS, it is important. And please read this answer again, he isn't suggestingdeclare -A account account["$ENV"]="$ENV_ACCOUNT"
.
– terdon♦
Jan 3 at 10:26
Sorry but this doesn't answer the question as it was set. The first line says 'I have to use environment vars' but this answer goes on with an array solution which completely ignores that requirement. I cannot claim to know OP's actual problem, but please consider a situation whereDEV_ACCOUNT
,STAGE_ACCOUNT
, etc. are given in the environment, their names cannot be changed, and the script has to choose and expand one of them based on a argument or another environment variable.
– mosvy
Jan 3 at 13:44
@mosvy See updated answer.
– Kusalananda
Jan 3 at 13:48
|
show 1 more comment
I will repeat what Kusalananda said about not using ALLCAPS for variable names in shell scripts. That is generally a bad idea and can cause issues when your variable names collide with default environment variables. And I also agree that the best way to do this is to use an associative array as he describes in his answer.
In any case, if you insist on doing it this (wrong) way instead of using an array, you will need to do something like this:
varname="$env"_account
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${!varname}:function:hello-world"
The ${!var}
syntax will expand to the value of a variable named var
. For example:
$ foo="foovar"
$ bar=foo
$ echo ${!bar}
foovar
Seriously though, just don't do this. It is complicated, cumbersome, hard to read, harder to maintain and an associative array will just solve all of these issues for you.
Why is$ENV_ACCOUNT
being parsed as${ENV}_ACCOUNT
instead of${ENV_ACCOUNT}
? Is it because$ENV
is special? (Or is that sufficiently interesting that it's better as a new question?)
– roaima
Jan 3 at 18:21
@roaima um. Because the OP said it did, so I assumed it was being treated as I described? I admit I just assumed that's what was happening without giving it much thought. Now that you mention it, I have no idea how the OP could have gotten this behavior instead of$ENV_ACCOUNT
being empty.
– terdon♦
Jan 3 at 18:51
@roaima I was assuming it was a mistyping in the question, or that they, at some point, setENV_ACCOUNT
to the stringDEV_ACCOUNT
and forgot about it. It was unimportant to answering the question (or so I thought).
– Kusalananda
Jan 3 at 18:58
@KusalanandaI haven't set the ENV_ACCOUNT to DEV_ACCOUNT. I had set DEV_ACCOUNT=12345678 as ENV
– Bharat
Jan 3 at 21:10
add a comment |
Thanks to everyone who posted solutions to my question. I had implemented bash arrays to solve my problem. But,My friend suggested a simple solution to it
printenv `echo $ENV`_ACCOUNT
solved my problem.
New contributor
Just printing the variable's value "solved it"?
– Kusalananda
15 hours ago
@Kusalananda Yeah, My main intention was to resolve the value of the variable. I redirected the output of "printenvecho $ENV
_ACCOUNT" to var and used that var in the command. But, thanks to you for telling about the bash arrays.
– Bharat
5 hours ago
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Bharat is a new contributor. Be nice, and check out our Code of Conduct.
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%2funix.stackexchange.com%2fquestions%2f492182%2fresolve-variable-in-a-variable-in-shell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Note that uppercase variable names are not generally safe to use. $ENV
, for example, is special in many shells. In these shells, its value should be the pathname of a file that will be sourced before starting certain types of shells. The bash
shell uses $ENV
in POSIX mode.
So, in bash
:
declare -A account
account["develop"]=12345678
account["staging"]=9128312
account["production"]=123123
mode=$run_mode
aws events put-targets
--rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${account[$mode]}:function:hello-wolrd"
This sets up an associative array, account
, that holds the data for development, staging and production modes. It then defines the mode that we're using and executes the command with the correct data from the account
array. The run_mode
variable is assumed to be an environment variable containing one of the strings develop
, staging
or production
.
Associative arrays were introduced in bash
4.0.
I have used your aws
command as you wrote it (except for adding newlines and my account
variable), even though the {{ stack_name | lower }}-hello-world
probably does not de what you'd expect it to (it would try to execute lower
as a command). Since I don't know aws
, I don't know what it should look like, but it should probably be quoted.
Addressing user mosvy's concerns in comments: In the case where the variables DEV_ACCOUNT
etc. are environment variables and the correct one has to be picked based on what the ENV
variable is:
declare -A account
account["DEV"]=$DEV_ACCOUNT
account["STAGE"]=$STAGE_ACCOUNT
account["PRD"]=$PRD_ACCOUNT
mode=$ENV
# the rest as before
Thanks for telling about arrays. Is there any other way to resolve the variable ? I tried the arrays using vars, but not successful. Instead of strings, declare -A account account["$ENV"]="$ENV_ACCOUNT" Didn't worked. Any suggestions?
– Bharat
Jan 3 at 8:45
@Bharat I don't think I ever used a variable while setting the values in theaccount
array in my code. Theaccount
array is an array of static values for the three typesdevelop
,staging
andproduction
. Themode
variable then controls which one of these that will be used.
– Kusalananda
Jan 3 at 9:11
@Bharat don't ignore the advice about not using CAPS, it is important. And please read this answer again, he isn't suggestingdeclare -A account account["$ENV"]="$ENV_ACCOUNT"
.
– terdon♦
Jan 3 at 10:26
Sorry but this doesn't answer the question as it was set. The first line says 'I have to use environment vars' but this answer goes on with an array solution which completely ignores that requirement. I cannot claim to know OP's actual problem, but please consider a situation whereDEV_ACCOUNT
,STAGE_ACCOUNT
, etc. are given in the environment, their names cannot be changed, and the script has to choose and expand one of them based on a argument or another environment variable.
– mosvy
Jan 3 at 13:44
@mosvy See updated answer.
– Kusalananda
Jan 3 at 13:48
|
show 1 more comment
Note that uppercase variable names are not generally safe to use. $ENV
, for example, is special in many shells. In these shells, its value should be the pathname of a file that will be sourced before starting certain types of shells. The bash
shell uses $ENV
in POSIX mode.
So, in bash
:
declare -A account
account["develop"]=12345678
account["staging"]=9128312
account["production"]=123123
mode=$run_mode
aws events put-targets
--rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${account[$mode]}:function:hello-wolrd"
This sets up an associative array, account
, that holds the data for development, staging and production modes. It then defines the mode that we're using and executes the command with the correct data from the account
array. The run_mode
variable is assumed to be an environment variable containing one of the strings develop
, staging
or production
.
Associative arrays were introduced in bash
4.0.
I have used your aws
command as you wrote it (except for adding newlines and my account
variable), even though the {{ stack_name | lower }}-hello-world
probably does not de what you'd expect it to (it would try to execute lower
as a command). Since I don't know aws
, I don't know what it should look like, but it should probably be quoted.
Addressing user mosvy's concerns in comments: In the case where the variables DEV_ACCOUNT
etc. are environment variables and the correct one has to be picked based on what the ENV
variable is:
declare -A account
account["DEV"]=$DEV_ACCOUNT
account["STAGE"]=$STAGE_ACCOUNT
account["PRD"]=$PRD_ACCOUNT
mode=$ENV
# the rest as before
Thanks for telling about arrays. Is there any other way to resolve the variable ? I tried the arrays using vars, but not successful. Instead of strings, declare -A account account["$ENV"]="$ENV_ACCOUNT" Didn't worked. Any suggestions?
– Bharat
Jan 3 at 8:45
@Bharat I don't think I ever used a variable while setting the values in theaccount
array in my code. Theaccount
array is an array of static values for the three typesdevelop
,staging
andproduction
. Themode
variable then controls which one of these that will be used.
– Kusalananda
Jan 3 at 9:11
@Bharat don't ignore the advice about not using CAPS, it is important. And please read this answer again, he isn't suggestingdeclare -A account account["$ENV"]="$ENV_ACCOUNT"
.
– terdon♦
Jan 3 at 10:26
Sorry but this doesn't answer the question as it was set. The first line says 'I have to use environment vars' but this answer goes on with an array solution which completely ignores that requirement. I cannot claim to know OP's actual problem, but please consider a situation whereDEV_ACCOUNT
,STAGE_ACCOUNT
, etc. are given in the environment, their names cannot be changed, and the script has to choose and expand one of them based on a argument or another environment variable.
– mosvy
Jan 3 at 13:44
@mosvy See updated answer.
– Kusalananda
Jan 3 at 13:48
|
show 1 more comment
Note that uppercase variable names are not generally safe to use. $ENV
, for example, is special in many shells. In these shells, its value should be the pathname of a file that will be sourced before starting certain types of shells. The bash
shell uses $ENV
in POSIX mode.
So, in bash
:
declare -A account
account["develop"]=12345678
account["staging"]=9128312
account["production"]=123123
mode=$run_mode
aws events put-targets
--rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${account[$mode]}:function:hello-wolrd"
This sets up an associative array, account
, that holds the data for development, staging and production modes. It then defines the mode that we're using and executes the command with the correct data from the account
array. The run_mode
variable is assumed to be an environment variable containing one of the strings develop
, staging
or production
.
Associative arrays were introduced in bash
4.0.
I have used your aws
command as you wrote it (except for adding newlines and my account
variable), even though the {{ stack_name | lower }}-hello-world
probably does not de what you'd expect it to (it would try to execute lower
as a command). Since I don't know aws
, I don't know what it should look like, but it should probably be quoted.
Addressing user mosvy's concerns in comments: In the case where the variables DEV_ACCOUNT
etc. are environment variables and the correct one has to be picked based on what the ENV
variable is:
declare -A account
account["DEV"]=$DEV_ACCOUNT
account["STAGE"]=$STAGE_ACCOUNT
account["PRD"]=$PRD_ACCOUNT
mode=$ENV
# the rest as before
Note that uppercase variable names are not generally safe to use. $ENV
, for example, is special in many shells. In these shells, its value should be the pathname of a file that will be sourced before starting certain types of shells. The bash
shell uses $ENV
in POSIX mode.
So, in bash
:
declare -A account
account["develop"]=12345678
account["staging"]=9128312
account["production"]=123123
mode=$run_mode
aws events put-targets
--rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${account[$mode]}:function:hello-wolrd"
This sets up an associative array, account
, that holds the data for development, staging and production modes. It then defines the mode that we're using and executes the command with the correct data from the account
array. The run_mode
variable is assumed to be an environment variable containing one of the strings develop
, staging
or production
.
Associative arrays were introduced in bash
4.0.
I have used your aws
command as you wrote it (except for adding newlines and my account
variable), even though the {{ stack_name | lower }}-hello-world
probably does not de what you'd expect it to (it would try to execute lower
as a command). Since I don't know aws
, I don't know what it should look like, but it should probably be quoted.
Addressing user mosvy's concerns in comments: In the case where the variables DEV_ACCOUNT
etc. are environment variables and the correct one has to be picked based on what the ENV
variable is:
declare -A account
account["DEV"]=$DEV_ACCOUNT
account["STAGE"]=$STAGE_ACCOUNT
account["PRD"]=$PRD_ACCOUNT
mode=$ENV
# the rest as before
edited Jan 3 at 13:48
answered Jan 3 at 8:18
KusalanandaKusalananda
122k16230375
122k16230375
Thanks for telling about arrays. Is there any other way to resolve the variable ? I tried the arrays using vars, but not successful. Instead of strings, declare -A account account["$ENV"]="$ENV_ACCOUNT" Didn't worked. Any suggestions?
– Bharat
Jan 3 at 8:45
@Bharat I don't think I ever used a variable while setting the values in theaccount
array in my code. Theaccount
array is an array of static values for the three typesdevelop
,staging
andproduction
. Themode
variable then controls which one of these that will be used.
– Kusalananda
Jan 3 at 9:11
@Bharat don't ignore the advice about not using CAPS, it is important. And please read this answer again, he isn't suggestingdeclare -A account account["$ENV"]="$ENV_ACCOUNT"
.
– terdon♦
Jan 3 at 10:26
Sorry but this doesn't answer the question as it was set. The first line says 'I have to use environment vars' but this answer goes on with an array solution which completely ignores that requirement. I cannot claim to know OP's actual problem, but please consider a situation whereDEV_ACCOUNT
,STAGE_ACCOUNT
, etc. are given in the environment, their names cannot be changed, and the script has to choose and expand one of them based on a argument or another environment variable.
– mosvy
Jan 3 at 13:44
@mosvy See updated answer.
– Kusalananda
Jan 3 at 13:48
|
show 1 more comment
Thanks for telling about arrays. Is there any other way to resolve the variable ? I tried the arrays using vars, but not successful. Instead of strings, declare -A account account["$ENV"]="$ENV_ACCOUNT" Didn't worked. Any suggestions?
– Bharat
Jan 3 at 8:45
@Bharat I don't think I ever used a variable while setting the values in theaccount
array in my code. Theaccount
array is an array of static values for the three typesdevelop
,staging
andproduction
. Themode
variable then controls which one of these that will be used.
– Kusalananda
Jan 3 at 9:11
@Bharat don't ignore the advice about not using CAPS, it is important. And please read this answer again, he isn't suggestingdeclare -A account account["$ENV"]="$ENV_ACCOUNT"
.
– terdon♦
Jan 3 at 10:26
Sorry but this doesn't answer the question as it was set. The first line says 'I have to use environment vars' but this answer goes on with an array solution which completely ignores that requirement. I cannot claim to know OP's actual problem, but please consider a situation whereDEV_ACCOUNT
,STAGE_ACCOUNT
, etc. are given in the environment, their names cannot be changed, and the script has to choose and expand one of them based on a argument or another environment variable.
– mosvy
Jan 3 at 13:44
@mosvy See updated answer.
– Kusalananda
Jan 3 at 13:48
Thanks for telling about arrays. Is there any other way to resolve the variable ? I tried the arrays using vars, but not successful. Instead of strings, declare -A account account["$ENV"]="$ENV_ACCOUNT" Didn't worked. Any suggestions?
– Bharat
Jan 3 at 8:45
Thanks for telling about arrays. Is there any other way to resolve the variable ? I tried the arrays using vars, but not successful. Instead of strings, declare -A account account["$ENV"]="$ENV_ACCOUNT" Didn't worked. Any suggestions?
– Bharat
Jan 3 at 8:45
@Bharat I don't think I ever used a variable while setting the values in the
account
array in my code. The account
array is an array of static values for the three types develop
, staging
and production
. The mode
variable then controls which one of these that will be used.– Kusalananda
Jan 3 at 9:11
@Bharat I don't think I ever used a variable while setting the values in the
account
array in my code. The account
array is an array of static values for the three types develop
, staging
and production
. The mode
variable then controls which one of these that will be used.– Kusalananda
Jan 3 at 9:11
@Bharat don't ignore the advice about not using CAPS, it is important. And please read this answer again, he isn't suggesting
declare -A account account["$ENV"]="$ENV_ACCOUNT"
.– terdon♦
Jan 3 at 10:26
@Bharat don't ignore the advice about not using CAPS, it is important. And please read this answer again, he isn't suggesting
declare -A account account["$ENV"]="$ENV_ACCOUNT"
.– terdon♦
Jan 3 at 10:26
Sorry but this doesn't answer the question as it was set. The first line says 'I have to use environment vars' but this answer goes on with an array solution which completely ignores that requirement. I cannot claim to know OP's actual problem, but please consider a situation where
DEV_ACCOUNT
, STAGE_ACCOUNT
, etc. are given in the environment, their names cannot be changed, and the script has to choose and expand one of them based on a argument or another environment variable.– mosvy
Jan 3 at 13:44
Sorry but this doesn't answer the question as it was set. The first line says 'I have to use environment vars' but this answer goes on with an array solution which completely ignores that requirement. I cannot claim to know OP's actual problem, but please consider a situation where
DEV_ACCOUNT
, STAGE_ACCOUNT
, etc. are given in the environment, their names cannot be changed, and the script has to choose and expand one of them based on a argument or another environment variable.– mosvy
Jan 3 at 13:44
@mosvy See updated answer.
– Kusalananda
Jan 3 at 13:48
@mosvy See updated answer.
– Kusalananda
Jan 3 at 13:48
|
show 1 more comment
I will repeat what Kusalananda said about not using ALLCAPS for variable names in shell scripts. That is generally a bad idea and can cause issues when your variable names collide with default environment variables. And I also agree that the best way to do this is to use an associative array as he describes in his answer.
In any case, if you insist on doing it this (wrong) way instead of using an array, you will need to do something like this:
varname="$env"_account
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${!varname}:function:hello-world"
The ${!var}
syntax will expand to the value of a variable named var
. For example:
$ foo="foovar"
$ bar=foo
$ echo ${!bar}
foovar
Seriously though, just don't do this. It is complicated, cumbersome, hard to read, harder to maintain and an associative array will just solve all of these issues for you.
Why is$ENV_ACCOUNT
being parsed as${ENV}_ACCOUNT
instead of${ENV_ACCOUNT}
? Is it because$ENV
is special? (Or is that sufficiently interesting that it's better as a new question?)
– roaima
Jan 3 at 18:21
@roaima um. Because the OP said it did, so I assumed it was being treated as I described? I admit I just assumed that's what was happening without giving it much thought. Now that you mention it, I have no idea how the OP could have gotten this behavior instead of$ENV_ACCOUNT
being empty.
– terdon♦
Jan 3 at 18:51
@roaima I was assuming it was a mistyping in the question, or that they, at some point, setENV_ACCOUNT
to the stringDEV_ACCOUNT
and forgot about it. It was unimportant to answering the question (or so I thought).
– Kusalananda
Jan 3 at 18:58
@KusalanandaI haven't set the ENV_ACCOUNT to DEV_ACCOUNT. I had set DEV_ACCOUNT=12345678 as ENV
– Bharat
Jan 3 at 21:10
add a comment |
I will repeat what Kusalananda said about not using ALLCAPS for variable names in shell scripts. That is generally a bad idea and can cause issues when your variable names collide with default environment variables. And I also agree that the best way to do this is to use an associative array as he describes in his answer.
In any case, if you insist on doing it this (wrong) way instead of using an array, you will need to do something like this:
varname="$env"_account
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${!varname}:function:hello-world"
The ${!var}
syntax will expand to the value of a variable named var
. For example:
$ foo="foovar"
$ bar=foo
$ echo ${!bar}
foovar
Seriously though, just don't do this. It is complicated, cumbersome, hard to read, harder to maintain and an associative array will just solve all of these issues for you.
Why is$ENV_ACCOUNT
being parsed as${ENV}_ACCOUNT
instead of${ENV_ACCOUNT}
? Is it because$ENV
is special? (Or is that sufficiently interesting that it's better as a new question?)
– roaima
Jan 3 at 18:21
@roaima um. Because the OP said it did, so I assumed it was being treated as I described? I admit I just assumed that's what was happening without giving it much thought. Now that you mention it, I have no idea how the OP could have gotten this behavior instead of$ENV_ACCOUNT
being empty.
– terdon♦
Jan 3 at 18:51
@roaima I was assuming it was a mistyping in the question, or that they, at some point, setENV_ACCOUNT
to the stringDEV_ACCOUNT
and forgot about it. It was unimportant to answering the question (or so I thought).
– Kusalananda
Jan 3 at 18:58
@KusalanandaI haven't set the ENV_ACCOUNT to DEV_ACCOUNT. I had set DEV_ACCOUNT=12345678 as ENV
– Bharat
Jan 3 at 21:10
add a comment |
I will repeat what Kusalananda said about not using ALLCAPS for variable names in shell scripts. That is generally a bad idea and can cause issues when your variable names collide with default environment variables. And I also agree that the best way to do this is to use an associative array as he describes in his answer.
In any case, if you insist on doing it this (wrong) way instead of using an array, you will need to do something like this:
varname="$env"_account
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${!varname}:function:hello-world"
The ${!var}
syntax will expand to the value of a variable named var
. For example:
$ foo="foovar"
$ bar=foo
$ echo ${!bar}
foovar
Seriously though, just don't do this. It is complicated, cumbersome, hard to read, harder to maintain and an associative array will just solve all of these issues for you.
I will repeat what Kusalananda said about not using ALLCAPS for variable names in shell scripts. That is generally a bad idea and can cause issues when your variable names collide with default environment variables. And I also agree that the best way to do this is to use an associative array as he describes in his answer.
In any case, if you insist on doing it this (wrong) way instead of using an array, you will need to do something like this:
varname="$env"_account
aws events put-targets --rule {{ stack_name | lower }}-hello-world
--targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:${!varname}:function:hello-world"
The ${!var}
syntax will expand to the value of a variable named var
. For example:
$ foo="foovar"
$ bar=foo
$ echo ${!bar}
foovar
Seriously though, just don't do this. It is complicated, cumbersome, hard to read, harder to maintain and an associative array will just solve all of these issues for you.
edited Jan 3 at 21:20
answered Jan 3 at 10:25
terdon♦terdon
129k31252427
129k31252427
Why is$ENV_ACCOUNT
being parsed as${ENV}_ACCOUNT
instead of${ENV_ACCOUNT}
? Is it because$ENV
is special? (Or is that sufficiently interesting that it's better as a new question?)
– roaima
Jan 3 at 18:21
@roaima um. Because the OP said it did, so I assumed it was being treated as I described? I admit I just assumed that's what was happening without giving it much thought. Now that you mention it, I have no idea how the OP could have gotten this behavior instead of$ENV_ACCOUNT
being empty.
– terdon♦
Jan 3 at 18:51
@roaima I was assuming it was a mistyping in the question, or that they, at some point, setENV_ACCOUNT
to the stringDEV_ACCOUNT
and forgot about it. It was unimportant to answering the question (or so I thought).
– Kusalananda
Jan 3 at 18:58
@KusalanandaI haven't set the ENV_ACCOUNT to DEV_ACCOUNT. I had set DEV_ACCOUNT=12345678 as ENV
– Bharat
Jan 3 at 21:10
add a comment |
Why is$ENV_ACCOUNT
being parsed as${ENV}_ACCOUNT
instead of${ENV_ACCOUNT}
? Is it because$ENV
is special? (Or is that sufficiently interesting that it's better as a new question?)
– roaima
Jan 3 at 18:21
@roaima um. Because the OP said it did, so I assumed it was being treated as I described? I admit I just assumed that's what was happening without giving it much thought. Now that you mention it, I have no idea how the OP could have gotten this behavior instead of$ENV_ACCOUNT
being empty.
– terdon♦
Jan 3 at 18:51
@roaima I was assuming it was a mistyping in the question, or that they, at some point, setENV_ACCOUNT
to the stringDEV_ACCOUNT
and forgot about it. It was unimportant to answering the question (or so I thought).
– Kusalananda
Jan 3 at 18:58
@KusalanandaI haven't set the ENV_ACCOUNT to DEV_ACCOUNT. I had set DEV_ACCOUNT=12345678 as ENV
– Bharat
Jan 3 at 21:10
Why is
$ENV_ACCOUNT
being parsed as ${ENV}_ACCOUNT
instead of ${ENV_ACCOUNT}
? Is it because $ENV
is special? (Or is that sufficiently interesting that it's better as a new question?)– roaima
Jan 3 at 18:21
Why is
$ENV_ACCOUNT
being parsed as ${ENV}_ACCOUNT
instead of ${ENV_ACCOUNT}
? Is it because $ENV
is special? (Or is that sufficiently interesting that it's better as a new question?)– roaima
Jan 3 at 18:21
@roaima um. Because the OP said it did, so I assumed it was being treated as I described? I admit I just assumed that's what was happening without giving it much thought. Now that you mention it, I have no idea how the OP could have gotten this behavior instead of
$ENV_ACCOUNT
being empty.– terdon♦
Jan 3 at 18:51
@roaima um. Because the OP said it did, so I assumed it was being treated as I described? I admit I just assumed that's what was happening without giving it much thought. Now that you mention it, I have no idea how the OP could have gotten this behavior instead of
$ENV_ACCOUNT
being empty.– terdon♦
Jan 3 at 18:51
@roaima I was assuming it was a mistyping in the question, or that they, at some point, set
ENV_ACCOUNT
to the string DEV_ACCOUNT
and forgot about it. It was unimportant to answering the question (or so I thought).– Kusalananda
Jan 3 at 18:58
@roaima I was assuming it was a mistyping in the question, or that they, at some point, set
ENV_ACCOUNT
to the string DEV_ACCOUNT
and forgot about it. It was unimportant to answering the question (or so I thought).– Kusalananda
Jan 3 at 18:58
@KusalanandaI haven't set the ENV_ACCOUNT to DEV_ACCOUNT. I had set DEV_ACCOUNT=12345678 as ENV
– Bharat
Jan 3 at 21:10
@KusalanandaI haven't set the ENV_ACCOUNT to DEV_ACCOUNT. I had set DEV_ACCOUNT=12345678 as ENV
– Bharat
Jan 3 at 21:10
add a comment |
Thanks to everyone who posted solutions to my question. I had implemented bash arrays to solve my problem. But,My friend suggested a simple solution to it
printenv `echo $ENV`_ACCOUNT
solved my problem.
New contributor
Just printing the variable's value "solved it"?
– Kusalananda
15 hours ago
@Kusalananda Yeah, My main intention was to resolve the value of the variable. I redirected the output of "printenvecho $ENV
_ACCOUNT" to var and used that var in the command. But, thanks to you for telling about the bash arrays.
– Bharat
5 hours ago
add a comment |
Thanks to everyone who posted solutions to my question. I had implemented bash arrays to solve my problem. But,My friend suggested a simple solution to it
printenv `echo $ENV`_ACCOUNT
solved my problem.
New contributor
Just printing the variable's value "solved it"?
– Kusalananda
15 hours ago
@Kusalananda Yeah, My main intention was to resolve the value of the variable. I redirected the output of "printenvecho $ENV
_ACCOUNT" to var and used that var in the command. But, thanks to you for telling about the bash arrays.
– Bharat
5 hours ago
add a comment |
Thanks to everyone who posted solutions to my question. I had implemented bash arrays to solve my problem. But,My friend suggested a simple solution to it
printenv `echo $ENV`_ACCOUNT
solved my problem.
New contributor
Thanks to everyone who posted solutions to my question. I had implemented bash arrays to solve my problem. But,My friend suggested a simple solution to it
printenv `echo $ENV`_ACCOUNT
solved my problem.
New contributor
edited 15 hours ago
Kusalananda
122k16230375
122k16230375
New contributor
answered 2 days ago
BharatBharat
143
143
New contributor
New contributor
Just printing the variable's value "solved it"?
– Kusalananda
15 hours ago
@Kusalananda Yeah, My main intention was to resolve the value of the variable. I redirected the output of "printenvecho $ENV
_ACCOUNT" to var and used that var in the command. But, thanks to you for telling about the bash arrays.
– Bharat
5 hours ago
add a comment |
Just printing the variable's value "solved it"?
– Kusalananda
15 hours ago
@Kusalananda Yeah, My main intention was to resolve the value of the variable. I redirected the output of "printenvecho $ENV
_ACCOUNT" to var and used that var in the command. But, thanks to you for telling about the bash arrays.
– Bharat
5 hours ago
Just printing the variable's value "solved it"?
– Kusalananda
15 hours ago
Just printing the variable's value "solved it"?
– Kusalananda
15 hours ago
@Kusalananda Yeah, My main intention was to resolve the value of the variable. I redirected the output of "printenv
echo $ENV
_ACCOUNT" to var and used that var in the command. But, thanks to you for telling about the bash arrays.– Bharat
5 hours ago
@Kusalananda Yeah, My main intention was to resolve the value of the variable. I redirected the output of "printenv
echo $ENV
_ACCOUNT" to var and used that var in the command. But, thanks to you for telling about the bash arrays.– Bharat
5 hours ago
add a comment |
Bharat is a new contributor. Be nice, and check out our Code of Conduct.
Bharat is a new contributor. Be nice, and check out our Code of Conduct.
Bharat is a new contributor. Be nice, and check out our Code of Conduct.
Bharat is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2funix.stackexchange.com%2fquestions%2f492182%2fresolve-variable-in-a-variable-in-shell%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
2
Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
Jan 3 at 8:02
And where (and to what) are you setting
ENV_ACCOUNT
?– tink
Jan 3 at 8:03
@P_Yadav aws events put-targets --rule {{ stack_name | lower }}-hello-world --targets "Id"="1","Arn"="arn:aws:lambda:us-west-2:$ENV_ACCOUNT:function:hello-wolrd" This is the command where i want to resolve the account number.
– Bharat
Jan 3 at 8:09
@Bharat please edit the question.
– P_Yadav
Jan 3 at 8:09