Difference between environment variables and exported environment variables in bash

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











up vote
36
down vote

favorite
11












Bash seems to differentiate between variables which have been exported and those which have not.



example:



$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR


set sees the variable but env does not.



$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR


set sees both variables but env sees only the exported variable.



I know that set is a bash builtin and env is not.



What are the differences between variables which are exported and those which are not?







share|improve this question

















  • 13




    Terminology note: an “environment variable” is always exported. A non-exported variable is a “shell variable” (or “parameter”).
    – Gilles
    Sep 13 '12 at 0:28














up vote
36
down vote

favorite
11












Bash seems to differentiate between variables which have been exported and those which have not.



example:



$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR


set sees the variable but env does not.



$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR


set sees both variables but env sees only the exported variable.



I know that set is a bash builtin and env is not.



What are the differences between variables which are exported and those which are not?







share|improve this question

















  • 13




    Terminology note: an “environment variable” is always exported. A non-exported variable is a “shell variable” (or “parameter”).
    – Gilles
    Sep 13 '12 at 0:28












up vote
36
down vote

favorite
11









up vote
36
down vote

favorite
11






11





Bash seems to differentiate between variables which have been exported and those which have not.



example:



$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR


set sees the variable but env does not.



$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR


set sees both variables but env sees only the exported variable.



I know that set is a bash builtin and env is not.



What are the differences between variables which are exported and those which are not?







share|improve this question













Bash seems to differentiate between variables which have been exported and those which have not.



example:



$ FOO=BAR
$ env | grep FOO
$ set | grep FOO
FOO=BAR


set sees the variable but env does not.



$ export BAR=FOO
$ env | grep FOO
BAR=FOO
$ set | grep FOO
BAR=FOO
FOO=BAR


set sees both variables but env sees only the exported variable.



I know that set is a bash builtin and env is not.



What are the differences between variables which are exported and those which are not?









share|improve this question












share|improve this question




share|improve this question








edited Dec 23 '12 at 14:59
























asked Oct 25 '10 at 22:15









lesmana

13.6k105469




13.6k105469







  • 13




    Terminology note: an “environment variable” is always exported. A non-exported variable is a “shell variable” (or “parameter”).
    – Gilles
    Sep 13 '12 at 0:28












  • 13




    Terminology note: an “environment variable” is always exported. A non-exported variable is a “shell variable” (or “parameter”).
    – Gilles
    Sep 13 '12 at 0:28







13




13




Terminology note: an “environment variable” is always exported. A non-exported variable is a “shell variable” (or “parameter”).
– Gilles
Sep 13 '12 at 0:28




Terminology note: an “environment variable” is always exported. A non-exported variable is a “shell variable” (or “parameter”).
– Gilles
Sep 13 '12 at 0:28










1 Answer
1






active

oldest

votes

















up vote
38
down vote



accepted










Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export man page:




The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.




set outputs the current environment, which includes any local non-exported variables. env is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$ to represent prompts in the inner shell):



$ FOO=BAR
$ bash
$$ echo $FOO # Note the empty line

$$ exit
$ export FOO
$ bash
$$ echo $FOO
BAR
$$


Edited to show that it's the variable itself that's exported, not the value. Once you export FOO, FOO becomes a global variable and shows up in subsequent environments, even if changed later:



$ export FOO
$ FOO=BAR
$ bash
$$ echo $FOO
BAR
$$





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',
    convertImagesToLinks: false,
    noModals: false,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    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%2funix.stackexchange.com%2fquestions%2f3507%2fdifference-between-environment-variables-and-exported-environment-variables-in-b%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
    38
    down vote



    accepted










    Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export man page:




    The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.




    set outputs the current environment, which includes any local non-exported variables. env is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$ to represent prompts in the inner shell):



    $ FOO=BAR
    $ bash
    $$ echo $FOO # Note the empty line

    $$ exit
    $ export FOO
    $ bash
    $$ echo $FOO
    BAR
    $$


    Edited to show that it's the variable itself that's exported, not the value. Once you export FOO, FOO becomes a global variable and shows up in subsequent environments, even if changed later:



    $ export FOO
    $ FOO=BAR
    $ bash
    $$ echo $FOO
    BAR
    $$





    share|improve this answer



























      up vote
      38
      down vote



      accepted










      Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export man page:




      The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.




      set outputs the current environment, which includes any local non-exported variables. env is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$ to represent prompts in the inner shell):



      $ FOO=BAR
      $ bash
      $$ echo $FOO # Note the empty line

      $$ exit
      $ export FOO
      $ bash
      $$ echo $FOO
      BAR
      $$


      Edited to show that it's the variable itself that's exported, not the value. Once you export FOO, FOO becomes a global variable and shows up in subsequent environments, even if changed later:



      $ export FOO
      $ FOO=BAR
      $ bash
      $$ echo $FOO
      BAR
      $$





      share|improve this answer

























        up vote
        38
        down vote



        accepted







        up vote
        38
        down vote



        accepted






        Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export man page:




        The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.




        set outputs the current environment, which includes any local non-exported variables. env is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$ to represent prompts in the inner shell):



        $ FOO=BAR
        $ bash
        $$ echo $FOO # Note the empty line

        $$ exit
        $ export FOO
        $ bash
        $$ echo $FOO
        BAR
        $$


        Edited to show that it's the variable itself that's exported, not the value. Once you export FOO, FOO becomes a global variable and shows up in subsequent environments, even if changed later:



        $ export FOO
        $ FOO=BAR
        $ bash
        $$ echo $FOO
        BAR
        $$





        share|improve this answer















        Exported variables are carried into the environment of processes started by the shell that exported them, while non-exported variables are local to the current process only. From the export man page:




        The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands.




        set outputs the current environment, which includes any local non-exported variables. env is used to launch programs in a new environment, and with no arguments will output what that new environment would be. Since env is creating a new environment, only exported variables are brought through, as is the case for any program launched from that shell. For example, spawning a second shell within the first (I used $$ to represent prompts in the inner shell):



        $ FOO=BAR
        $ bash
        $$ echo $FOO # Note the empty line

        $$ exit
        $ export FOO
        $ bash
        $$ echo $FOO
        BAR
        $$


        Edited to show that it's the variable itself that's exported, not the value. Once you export FOO, FOO becomes a global variable and shows up in subsequent environments, even if changed later:



        $ export FOO
        $ FOO=BAR
        $ bash
        $$ echo $FOO
        BAR
        $$






        share|improve this answer















        share|improve this answer



        share|improve this answer








        edited May 5 '15 at 5:16









        Azhrei

        33818




        33818











        answered Oct 25 '10 at 22:25









        Michael Mrozek♦

        58.1k26184206




        58.1k26184206






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f3507%2fdifference-between-environment-variables-and-exported-environment-variables-in-b%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Displaying single band from multi-band raster using QGIS

            How many registers does an x86_64 CPU actually have?