How to override local variable by environment variable












0














I have one script running in production where one value assigns but i want to use a different value from here.



My script.sh



a=20181214

....


I was calling this script after export a=20181212 and then call this script as i want to use 20181212 from this script.
But each time its picking the value as 20181214.










share|improve this question
























  • Can you modify the script?
    – muru
    Dec 17 at 6:00
















0














I have one script running in production where one value assigns but i want to use a different value from here.



My script.sh



a=20181214

....


I was calling this script after export a=20181212 and then call this script as i want to use 20181212 from this script.
But each time its picking the value as 20181214.










share|improve this question
























  • Can you modify the script?
    – muru
    Dec 17 at 6:00














0












0








0







I have one script running in production where one value assigns but i want to use a different value from here.



My script.sh



a=20181214

....


I was calling this script after export a=20181212 and then call this script as i want to use 20181212 from this script.
But each time its picking the value as 20181214.










share|improve this question















I have one script running in production where one value assigns but i want to use a different value from here.



My script.sh



a=20181214

....


I was calling this script after export a=20181212 and then call this script as i want to use 20181212 from this script.
But each time its picking the value as 20181214.







linux shell-script environment-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 17 at 17:50









Kusalananda

121k16229372




121k16229372










asked Dec 17 at 5:30









Mahibarauniya

32




32












  • Can you modify the script?
    – muru
    Dec 17 at 6:00


















  • Can you modify the script?
    – muru
    Dec 17 at 6:00
















Can you modify the script?
– muru
Dec 17 at 6:00




Can you modify the script?
– muru
Dec 17 at 6:00










2 Answers
2






active

oldest

votes


















0














If you can modify the script then modify it so that it says



a=${a:-20181214}


This would make it use the previously set value of a, or 20181214 if $a is empty or the variable is unset. This is a standard parameter expansion.



You would then either use



export a=20181212
./script.sh


or



a=20181212 ./script.sh


The latter of these avoids setting the variable in the calling environment and only sets it for the script's environment.



If you can't modify the script, then the script would always override your a value, no matter what you did. In this case, contact the person who maintains the script and explain the situation. If worse comes to worse, use a copy of the script that you can modify, assuming the script does not expect to be located in a particular location.






share|improve this answer























  • Depending on the interpreter, there are possible hack approaches like with $BASH_ENV/~/.zshenv where you could add some DEBUG traps, or bash's exported functions where you could redefine a command used in the script to reset the value of the variable to the one the OP wants after it has run the a=20181214 assignment.
    – Stéphane Chazelas
    Dec 17 at 7:47



















0














A script will be executed within another shell. And the command export A only sets the variable for the current shell.
You could export the variable on top of your script, it wouldn't affect the rest of your environment. Eg



~$ export A=a
~$ echo $A
a


if you create in the same shell a script test.sh



#!/bin/bash
export A=b
echo $A


The output will be 'b', but value of A in the shell remains unchanged






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
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f489407%2fhow-to-override-local-variable-by-environment-variable%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









    0














    If you can modify the script then modify it so that it says



    a=${a:-20181214}


    This would make it use the previously set value of a, or 20181214 if $a is empty or the variable is unset. This is a standard parameter expansion.



    You would then either use



    export a=20181212
    ./script.sh


    or



    a=20181212 ./script.sh


    The latter of these avoids setting the variable in the calling environment and only sets it for the script's environment.



    If you can't modify the script, then the script would always override your a value, no matter what you did. In this case, contact the person who maintains the script and explain the situation. If worse comes to worse, use a copy of the script that you can modify, assuming the script does not expect to be located in a particular location.






    share|improve this answer























    • Depending on the interpreter, there are possible hack approaches like with $BASH_ENV/~/.zshenv where you could add some DEBUG traps, or bash's exported functions where you could redefine a command used in the script to reset the value of the variable to the one the OP wants after it has run the a=20181214 assignment.
      – Stéphane Chazelas
      Dec 17 at 7:47
















    0














    If you can modify the script then modify it so that it says



    a=${a:-20181214}


    This would make it use the previously set value of a, or 20181214 if $a is empty or the variable is unset. This is a standard parameter expansion.



    You would then either use



    export a=20181212
    ./script.sh


    or



    a=20181212 ./script.sh


    The latter of these avoids setting the variable in the calling environment and only sets it for the script's environment.



    If you can't modify the script, then the script would always override your a value, no matter what you did. In this case, contact the person who maintains the script and explain the situation. If worse comes to worse, use a copy of the script that you can modify, assuming the script does not expect to be located in a particular location.






    share|improve this answer























    • Depending on the interpreter, there are possible hack approaches like with $BASH_ENV/~/.zshenv where you could add some DEBUG traps, or bash's exported functions where you could redefine a command used in the script to reset the value of the variable to the one the OP wants after it has run the a=20181214 assignment.
      – Stéphane Chazelas
      Dec 17 at 7:47














    0












    0








    0






    If you can modify the script then modify it so that it says



    a=${a:-20181214}


    This would make it use the previously set value of a, or 20181214 if $a is empty or the variable is unset. This is a standard parameter expansion.



    You would then either use



    export a=20181212
    ./script.sh


    or



    a=20181212 ./script.sh


    The latter of these avoids setting the variable in the calling environment and only sets it for the script's environment.



    If you can't modify the script, then the script would always override your a value, no matter what you did. In this case, contact the person who maintains the script and explain the situation. If worse comes to worse, use a copy of the script that you can modify, assuming the script does not expect to be located in a particular location.






    share|improve this answer














    If you can modify the script then modify it so that it says



    a=${a:-20181214}


    This would make it use the previously set value of a, or 20181214 if $a is empty or the variable is unset. This is a standard parameter expansion.



    You would then either use



    export a=20181212
    ./script.sh


    or



    a=20181212 ./script.sh


    The latter of these avoids setting the variable in the calling environment and only sets it for the script's environment.



    If you can't modify the script, then the script would always override your a value, no matter what you did. In this case, contact the person who maintains the script and explain the situation. If worse comes to worse, use a copy of the script that you can modify, assuming the script does not expect to be located in a particular location.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Dec 17 at 7:39

























    answered Dec 17 at 7:33









    Kusalananda

    121k16229372




    121k16229372












    • Depending on the interpreter, there are possible hack approaches like with $BASH_ENV/~/.zshenv where you could add some DEBUG traps, or bash's exported functions where you could redefine a command used in the script to reset the value of the variable to the one the OP wants after it has run the a=20181214 assignment.
      – Stéphane Chazelas
      Dec 17 at 7:47


















    • Depending on the interpreter, there are possible hack approaches like with $BASH_ENV/~/.zshenv where you could add some DEBUG traps, or bash's exported functions where you could redefine a command used in the script to reset the value of the variable to the one the OP wants after it has run the a=20181214 assignment.
      – Stéphane Chazelas
      Dec 17 at 7:47
















    Depending on the interpreter, there are possible hack approaches like with $BASH_ENV/~/.zshenv where you could add some DEBUG traps, or bash's exported functions where you could redefine a command used in the script to reset the value of the variable to the one the OP wants after it has run the a=20181214 assignment.
    – Stéphane Chazelas
    Dec 17 at 7:47




    Depending on the interpreter, there are possible hack approaches like with $BASH_ENV/~/.zshenv where you could add some DEBUG traps, or bash's exported functions where you could redefine a command used in the script to reset the value of the variable to the one the OP wants after it has run the a=20181214 assignment.
    – Stéphane Chazelas
    Dec 17 at 7:47













    0














    A script will be executed within another shell. And the command export A only sets the variable for the current shell.
    You could export the variable on top of your script, it wouldn't affect the rest of your environment. Eg



    ~$ export A=a
    ~$ echo $A
    a


    if you create in the same shell a script test.sh



    #!/bin/bash
    export A=b
    echo $A


    The output will be 'b', but value of A in the shell remains unchanged






    share|improve this answer


























      0














      A script will be executed within another shell. And the command export A only sets the variable for the current shell.
      You could export the variable on top of your script, it wouldn't affect the rest of your environment. Eg



      ~$ export A=a
      ~$ echo $A
      a


      if you create in the same shell a script test.sh



      #!/bin/bash
      export A=b
      echo $A


      The output will be 'b', but value of A in the shell remains unchanged






      share|improve this answer
























        0












        0








        0






        A script will be executed within another shell. And the command export A only sets the variable for the current shell.
        You could export the variable on top of your script, it wouldn't affect the rest of your environment. Eg



        ~$ export A=a
        ~$ echo $A
        a


        if you create in the same shell a script test.sh



        #!/bin/bash
        export A=b
        echo $A


        The output will be 'b', but value of A in the shell remains unchanged






        share|improve this answer












        A script will be executed within another shell. And the command export A only sets the variable for the current shell.
        You could export the variable on top of your script, it wouldn't affect the rest of your environment. Eg



        ~$ export A=a
        ~$ echo $A
        a


        if you create in the same shell a script test.sh



        #!/bin/bash
        export A=b
        echo $A


        The output will be 'b', but value of A in the shell remains unchanged







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 17 at 10:19









        Neah-Ko

        335




        335






























            draft saved

            draft discarded




















































            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%2f489407%2fhow-to-override-local-variable-by-environment-variable%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

            Morgemoulin

            Scott Moir

            Souastre