Resolve Variable in a Variable in shell












2














I have to use environment vars in my bash shell and:





  • echo $ENV resolves to DEV, STAGE or PRD depending upon the stack.


  • echo $DEV_ACCOUNT resolves to a number e.g. echo $DEV_ACCOUNT gives 12345678


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?










share|improve this question









New contributor




Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Also provide the command where you are using ENV_ACCOUNT.
    – P_Yadav
    2 hours ago










  • And where (and to what) are you setting ENV_ACCOUNT?
    – tink
    2 hours ago










  • @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
    2 hours ago










  • @Bharat please edit the question.
    – P_Yadav
    2 hours ago
















2














I have to use environment vars in my bash shell and:





  • echo $ENV resolves to DEV, STAGE or PRD depending upon the stack.


  • echo $DEV_ACCOUNT resolves to a number e.g. echo $DEV_ACCOUNT gives 12345678


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?










share|improve this question









New contributor




Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 2




    Also provide the command where you are using ENV_ACCOUNT.
    – P_Yadav
    2 hours ago










  • And where (and to what) are you setting ENV_ACCOUNT?
    – tink
    2 hours ago










  • @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
    2 hours ago










  • @Bharat please edit the question.
    – P_Yadav
    2 hours ago














2












2








2







I have to use environment vars in my bash shell and:





  • echo $ENV resolves to DEV, STAGE or PRD depending upon the stack.


  • echo $DEV_ACCOUNT resolves to a number e.g. echo $DEV_ACCOUNT gives 12345678


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?










share|improve this question









New contributor




Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have to use environment vars in my bash shell and:





  • echo $ENV resolves to DEV, STAGE or PRD depending upon the stack.


  • echo $DEV_ACCOUNT resolves to a number e.g. echo $DEV_ACCOUNT gives 12345678


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






share|improve this question









New contributor




Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 1 hour ago









nohillside

2,372819




2,372819






New contributor




Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 2 hours ago









Bharat

112




112




New contributor




Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Bharat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 2




    Also provide the command where you are using ENV_ACCOUNT.
    – P_Yadav
    2 hours ago










  • And where (and to what) are you setting ENV_ACCOUNT?
    – tink
    2 hours ago










  • @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
    2 hours ago










  • @Bharat please edit the question.
    – P_Yadav
    2 hours ago














  • 2




    Also provide the command where you are using ENV_ACCOUNT.
    – P_Yadav
    2 hours ago










  • And where (and to what) are you setting ENV_ACCOUNT?
    – tink
    2 hours ago










  • @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
    2 hours ago










  • @Bharat please edit the question.
    – P_Yadav
    2 hours ago








2




2




Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
2 hours ago




Also provide the command where you are using ENV_ACCOUNT.
– P_Yadav
2 hours ago












And where (and to what) are you setting ENV_ACCOUNT?
– tink
2 hours ago




And where (and to what) are you setting ENV_ACCOUNT?
– tink
2 hours ago












@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
2 hours ago




@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
2 hours ago












@Bharat please edit the question.
– P_Yadav
2 hours ago




@Bharat please edit the question.
– P_Yadav
2 hours ago










2 Answers
2






active

oldest

votes


















3














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.






share|improve this answer























  • 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
    2 hours ago












  • @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
    1 hour ago










  • @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
    22 mins ago



















1














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.



That said, the specific problem you are facing is because $ENV_ACCOUNT is being read as $ENV and the string _ACCOUNT. So $ENV is being expanded to DEV and $ENV_ACCOUNT becomes DEV_ACCOUNT. 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.






share|improve this answer





















    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.










    draft saved

    draft discarded


















    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

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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.






    share|improve this answer























    • 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
      2 hours ago












    • @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
      1 hour ago










    • @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
      22 mins ago
















    3














    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.






    share|improve this answer























    • 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
      2 hours ago












    • @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
      1 hour ago










    • @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
      22 mins ago














    3












    3








    3






    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.






    share|improve this answer














    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited 1 hour ago

























    answered 2 hours ago









    Kusalananda

    122k16229374




    122k16229374












    • 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
      2 hours ago












    • @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
      1 hour ago










    • @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
      22 mins ago


















    • 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
      2 hours ago












    • @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
      1 hour ago










    • @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
      22 mins ago
















    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
    2 hours ago






    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
    2 hours ago














    @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
    1 hour ago




    @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
    1 hour ago












    @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
    22 mins ago




    @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
    22 mins ago













    1














    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.



    That said, the specific problem you are facing is because $ENV_ACCOUNT is being read as $ENV and the string _ACCOUNT. So $ENV is being expanded to DEV and $ENV_ACCOUNT becomes DEV_ACCOUNT. 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.






    share|improve this answer


























      1














      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.



      That said, the specific problem you are facing is because $ENV_ACCOUNT is being read as $ENV and the string _ACCOUNT. So $ENV is being expanded to DEV and $ENV_ACCOUNT becomes DEV_ACCOUNT. 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.






      share|improve this answer
























        1












        1








        1






        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.



        That said, the specific problem you are facing is because $ENV_ACCOUNT is being read as $ENV and the string _ACCOUNT. So $ENV is being expanded to DEV and $ENV_ACCOUNT becomes DEV_ACCOUNT. 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.






        share|improve this answer












        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.



        That said, the specific problem you are facing is because $ENV_ACCOUNT is being read as $ENV and the string _ACCOUNT. So $ENV is being expanded to DEV and $ENV_ACCOUNT becomes DEV_ACCOUNT. 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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 23 mins ago









        terdon

        128k31249423




        128k31249423






















            Bharat is a new contributor. Be nice, and check out our Code of Conduct.










            draft saved

            draft discarded


















            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.




            draft saved


            draft discarded














            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





















































            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

            Understanding the information contained in the Deep Space Network XML data?

            Ross-on-Wye

            Eastern Orthodox Church