How to set a global persistent variable?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP








up vote
2
down vote

favorite
2












I'm creating a bashscript to setup a small application I'm writing. Part of that setup requires setting an environment variable so that other processes may reference it.



My simple shell script test looks like this:



#!/bin/sh

export FOO=abc


I understand why FOO will not be visible after the script is executed, if done like so:



$ sh script.sh


and why this will make it visible:



$ source script.sh


so that's a workaround, but I'm wondering how common install scripts do this - for example, I'm pretty sure installers like mysql etc do set global environment variables.



What is the right way to do this? I guess I can ask my users to install by running:



$ source script.sh


but not sure if there is a better way.



Thanks










share|improve this question





















  • Possible solutions: askubuntu.com/questions/58814/…
    – Terrance
    Mar 21 at 17:34














up vote
2
down vote

favorite
2












I'm creating a bashscript to setup a small application I'm writing. Part of that setup requires setting an environment variable so that other processes may reference it.



My simple shell script test looks like this:



#!/bin/sh

export FOO=abc


I understand why FOO will not be visible after the script is executed, if done like so:



$ sh script.sh


and why this will make it visible:



$ source script.sh


so that's a workaround, but I'm wondering how common install scripts do this - for example, I'm pretty sure installers like mysql etc do set global environment variables.



What is the right way to do this? I guess I can ask my users to install by running:



$ source script.sh


but not sure if there is a better way.



Thanks










share|improve this question





















  • Possible solutions: askubuntu.com/questions/58814/…
    – Terrance
    Mar 21 at 17:34












up vote
2
down vote

favorite
2









up vote
2
down vote

favorite
2






2





I'm creating a bashscript to setup a small application I'm writing. Part of that setup requires setting an environment variable so that other processes may reference it.



My simple shell script test looks like this:



#!/bin/sh

export FOO=abc


I understand why FOO will not be visible after the script is executed, if done like so:



$ sh script.sh


and why this will make it visible:



$ source script.sh


so that's a workaround, but I'm wondering how common install scripts do this - for example, I'm pretty sure installers like mysql etc do set global environment variables.



What is the right way to do this? I guess I can ask my users to install by running:



$ source script.sh


but not sure if there is a better way.



Thanks










share|improve this question













I'm creating a bashscript to setup a small application I'm writing. Part of that setup requires setting an environment variable so that other processes may reference it.



My simple shell script test looks like this:



#!/bin/sh

export FOO=abc


I understand why FOO will not be visible after the script is executed, if done like so:



$ sh script.sh


and why this will make it visible:



$ source script.sh


so that's a workaround, but I'm wondering how common install scripts do this - for example, I'm pretty sure installers like mysql etc do set global environment variables.



What is the right way to do this? I guess I can ask my users to install by running:



$ source script.sh


but not sure if there is a better way.



Thanks







bash scripts environment-variables






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 21 at 17:30









user3203425

1345




1345











  • Possible solutions: askubuntu.com/questions/58814/…
    – Terrance
    Mar 21 at 17:34
















  • Possible solutions: askubuntu.com/questions/58814/…
    – Terrance
    Mar 21 at 17:34















Possible solutions: askubuntu.com/questions/58814/…
– Terrance
Mar 21 at 17:34




Possible solutions: askubuntu.com/questions/58814/…
– Terrance
Mar 21 at 17:34










1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










Add these lines to your script. Your script should have root access.



echo 'export FOO=abc' > /etc/profile.d/foo.sh


Next restart it will be available.






share|improve this answer




















    Your Answer







    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "89"
    ;
    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',
    convertImagesToLinks: true,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













     

    draft saved


    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1018014%2fhow-to-set-a-global-persistent-variable%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote



    accepted










    Add these lines to your script. Your script should have root access.



    echo 'export FOO=abc' > /etc/profile.d/foo.sh


    Next restart it will be available.






    share|improve this answer
























      up vote
      1
      down vote



      accepted










      Add these lines to your script. Your script should have root access.



      echo 'export FOO=abc' > /etc/profile.d/foo.sh


      Next restart it will be available.






      share|improve this answer






















        up vote
        1
        down vote



        accepted







        up vote
        1
        down vote



        accepted






        Add these lines to your script. Your script should have root access.



        echo 'export FOO=abc' > /etc/profile.d/foo.sh


        Next restart it will be available.






        share|improve this answer












        Add these lines to your script. Your script should have root access.



        echo 'export FOO=abc' > /etc/profile.d/foo.sh


        Next restart it will be available.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 21 at 17:40









        dedunumax

        4,26021526




        4,26021526



























             

            draft saved


            draft discarded















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1018014%2fhow-to-set-a-global-persistent-variable%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            Trouble downloading packages list due to a “Hash sum mismatch” error

            How do so many people here on Academia.SE, and in general, afford lavish higher education programs?

            Cutting all the characters after the last /