Why does FOO=bar; export the variable into my environmentBash: why isn't “set” behaving like I expect it to?$PATH duplication issuesWhy does export -p exclude $_ variable?systemd: default value for environment variableChange Environment Path Variable OrderHow prevent users from running commands via dot slash for a directory?How to prevent the caller's shell from being used in sudo`env foo=bar echo $foo` prints nothinglogin loop after editing /etc/profile and /etc/login.defsWhat does `echo $_` output after `export foo=bar`?

Why'd a rational buyer offer to buy with no conditions precedent?

Why isn't Tyrion mentioned in 'A song of Ice and Fire'?

Why was this character made Grand Maester?

What is to the west of Westeros?

Are there historical examples of audiences drawn to a work that was "so bad it's good"?

Is there an idiom that means that you are in a very strong negotiation position in a negotiation?

Cisco 3750X Power Cable

Why is unzipped directory exactly 4.0K (much smaller than zipped file)?

Are there any German nonsense poems (Jabberwocky)?

Are there guidelines for finding good names for LaTeX 2e packages and control sequences defined in these packages?

What is the purpose of the yellow wired panels on the IBM 360 Model 20?

Complications of displaced core material?

Knight's Tour on a 7x7 Board starting from D5

Is it normal to "extract a paper" from a master thesis?

Could a rotating ring space station have a bolo-like extension?

Can diplomats be allowed on the flight deck of a commercial European airline?

What did Brienne write about Jaime?

Are runways booked by airlines to land their planes?

Ribbon Cable Cross Talk - Is there a fix after the fact?

What did the 'turbo' button actually do?

Is a world with one country feeding everyone possible?

The disk image is 497GB smaller than the target device

Status of proof by contradiction and excluded middle throughout the history of mathematics?

How does Dreadhorde Arcanist interact with split cards?



Why does FOO=bar; export the variable into my environment


Bash: why isn't “set” behaving like I expect it to?$PATH duplication issuesWhy does export -p exclude $_ variable?systemd: default value for environment variableChange Environment Path Variable OrderHow prevent users from running commands via dot slash for a directory?How to prevent the caller's shell from being used in sudo`env foo=bar echo $foo` prints nothinglogin loop after editing /etc/profile and /etc/login.defsWhat does `echo $_` output after `export foo=bar`?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















If I run



FOO=bar docker run -it -e FOO=$FOO debian env


That environment variable is not set in the command output for the env command.



PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=03f3b59c0aab
TERM=xterm
FOO=
HOME=/root


But if I run



FOO=bar; docker run -i -t --rm -e FOO=$FOO debian:stable-slim env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=672bfdcde93c
TERM=xterm
FOO=bar
HOME=/root


Then the variable is available from the container and also exported into my current shell environment.



echo $FOO
bar


I expect this behavior with export FOO=bar but why does that happen with ; too?










share|improve this question



















  • 5





    Are you sure your examples are correct? If I do foo=bar then run echo "$foo" I see bar, are you trying to do foo=bar some_other_command in the first example?

    – Eric Renouf
    3 hours ago











  • digging into it more it doesn't look like the ; matters until I run bash again. I'll clarify the question with a more concrete example of what I was trying to do.

    – Rothgar
    3 hours ago











  • What shell are you using? OS? Note: environment variables are listed by env for example. A simple echo "$var" prints the value of a variable (environment or not).

    – Isaac
    3 hours ago

















1















If I run



FOO=bar docker run -it -e FOO=$FOO debian env


That environment variable is not set in the command output for the env command.



PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=03f3b59c0aab
TERM=xterm
FOO=
HOME=/root


But if I run



FOO=bar; docker run -i -t --rm -e FOO=$FOO debian:stable-slim env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=672bfdcde93c
TERM=xterm
FOO=bar
HOME=/root


Then the variable is available from the container and also exported into my current shell environment.



echo $FOO
bar


I expect this behavior with export FOO=bar but why does that happen with ; too?










share|improve this question



















  • 5





    Are you sure your examples are correct? If I do foo=bar then run echo "$foo" I see bar, are you trying to do foo=bar some_other_command in the first example?

    – Eric Renouf
    3 hours ago











  • digging into it more it doesn't look like the ; matters until I run bash again. I'll clarify the question with a more concrete example of what I was trying to do.

    – Rothgar
    3 hours ago











  • What shell are you using? OS? Note: environment variables are listed by env for example. A simple echo "$var" prints the value of a variable (environment or not).

    – Isaac
    3 hours ago













1












1








1








If I run



FOO=bar docker run -it -e FOO=$FOO debian env


That environment variable is not set in the command output for the env command.



PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=03f3b59c0aab
TERM=xterm
FOO=
HOME=/root


But if I run



FOO=bar; docker run -i -t --rm -e FOO=$FOO debian:stable-slim env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=672bfdcde93c
TERM=xterm
FOO=bar
HOME=/root


Then the variable is available from the container and also exported into my current shell environment.



echo $FOO
bar


I expect this behavior with export FOO=bar but why does that happen with ; too?










share|improve this question
















If I run



FOO=bar docker run -it -e FOO=$FOO debian env


That environment variable is not set in the command output for the env command.



PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=03f3b59c0aab
TERM=xterm
FOO=
HOME=/root


But if I run



FOO=bar; docker run -i -t --rm -e FOO=$FOO debian:stable-slim env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=672bfdcde93c
TERM=xterm
FOO=bar
HOME=/root


Then the variable is available from the container and also exported into my current shell environment.



echo $FOO
bar


I expect this behavior with export FOO=bar but why does that happen with ; too?







bash shell environment-variables






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 3 hours ago







Rothgar

















asked 3 hours ago









RothgarRothgar

2,50031317




2,50031317







  • 5





    Are you sure your examples are correct? If I do foo=bar then run echo "$foo" I see bar, are you trying to do foo=bar some_other_command in the first example?

    – Eric Renouf
    3 hours ago











  • digging into it more it doesn't look like the ; matters until I run bash again. I'll clarify the question with a more concrete example of what I was trying to do.

    – Rothgar
    3 hours ago











  • What shell are you using? OS? Note: environment variables are listed by env for example. A simple echo "$var" prints the value of a variable (environment or not).

    – Isaac
    3 hours ago












  • 5





    Are you sure your examples are correct? If I do foo=bar then run echo "$foo" I see bar, are you trying to do foo=bar some_other_command in the first example?

    – Eric Renouf
    3 hours ago











  • digging into it more it doesn't look like the ; matters until I run bash again. I'll clarify the question with a more concrete example of what I was trying to do.

    – Rothgar
    3 hours ago











  • What shell are you using? OS? Note: environment variables are listed by env for example. A simple echo "$var" prints the value of a variable (environment or not).

    – Isaac
    3 hours ago







5




5





Are you sure your examples are correct? If I do foo=bar then run echo "$foo" I see bar, are you trying to do foo=bar some_other_command in the first example?

– Eric Renouf
3 hours ago





Are you sure your examples are correct? If I do foo=bar then run echo "$foo" I see bar, are you trying to do foo=bar some_other_command in the first example?

– Eric Renouf
3 hours ago













digging into it more it doesn't look like the ; matters until I run bash again. I'll clarify the question with a more concrete example of what I was trying to do.

– Rothgar
3 hours ago





digging into it more it doesn't look like the ; matters until I run bash again. I'll clarify the question with a more concrete example of what I was trying to do.

– Rothgar
3 hours ago













What shell are you using? OS? Note: environment variables are listed by env for example. A simple echo "$var" prints the value of a variable (environment or not).

– Isaac
3 hours ago





What shell are you using? OS? Note: environment variables are listed by env for example. A simple echo "$var" prints the value of a variable (environment or not).

– Isaac
3 hours ago










3 Answers
3






active

oldest

votes


















4














No, FOO=bar; does not export the variable into my environment



A var is set in the (present) environment only if it was previously exported:



$ export foo
$ foo=bar
$ env | grep foo
foo=bar


A variable is set in the environment of a command when it is placed before the command. Like foo=bar command. And it only exists while the command runs.



$ foo=bar bash -c 'echo "foo is = $foo"'
foo is = bar


The var is not set for the command line (in the present shell):



$ foo=bar bash -c echo $foo


Above, the value of $foo is replaced with nothing by the present running shell, thus: no output.



Your command:



$ FOO=bar docker run -it -e FOO=$FOO debian env


is converted to the actual string:



$ FOO=bar docker run -it -e FOO= debian env


by the present running shell.



If, instead, you set the variable (in the present running shell) with foo=bar before running the command, the line will be converted to:



$ FOO=bar; docker run -it -e FOO=bar debian env


A variable set to the environment of a command is erased when the command returns:



$ foo=bar bash -c 'echo'; echo "foo was erased: "$foo""


Except when the command is a builtin in some conditions/shells:



$ ksh -c 'foo=bar typeset baz=quuz; echo $foo'
bar





share|improve this answer




















  • 1





    The variable FOO is also not exported to the current shell environment, but set as a shell variable, unless it has been previously exported.

    – Johan Myréen
    2 hours ago











  • Not knowing anything about Docker, I wonder if it would actually work if the -e FOO=$FOO thing (which I presume is used to set an environment variable) is removed since FOO is set in Docker's environment.

    – Kusalananda
    2 hours ago












  • Not being fluent in docker language, I'll refrain from making any recomendation. But yes, what you comment seems reasonable. @Kusalananda

    – Isaac
    2 hours ago











  • @JohanMyréen Yes, Where I stated the contrary?

    – Isaac
    2 hours ago


















3














There are a number of variations to consider:




  1. Just doing FOO=bar creates a variable named FOO with value bar, but that variable isn't passed along to new processes:



    $ echo $FOO
    $ FOO=bar
    $ echo $FOO
    bar
    $ bash # Start a new bash process
    $ echo $FOO
    # Variable is not set in the new process
    $ exit # Exit new bash process



  2. Running FOO=bar <command> will run the given command with the variable set (but doesn't affect the original shell's environment):



    $ echo $foo
    $ FOO=baz bash # start a new bash process
    $ echo $FOO
    baz
    $ exit # exit the new bash process
    exit
    $ echo $FOO
    # No FOO in the original bash process
    $



  3. Doing FOO=foo; <command> is equivalent to (1); putting a semicolon between two commands is equivalent to running those two commands on two separate lines:



    $ FOO=foo; echo $FOO
    foo
    $ bash
    $ echo $FOO

    $ exit
    exit
    $ echo $FOO
    foo
    $



  4. Using export will pass a variable in the shell's environment to newly-created processes:



    $ export FOO=bar
    $ echo $FOO # Value set in original shell
    bar
    $ bash # Start another shell
    $ echo $FOO
    bar # Value was passed along to new process
    $ exit
    exit






share|improve this answer






























    0















    FOO=bar docker run -it -e FOO=$FOO debian env




    Here the $FOO from FOO=$FOO will be expanded before the FOO=bar assignment happens.



    You can check that with a more straight-forward example:



    FOO=first
    FOO=second echo FOO=$FOO
    => FOO=first

    FOO=third; echo FOO=$FOO
    => FOO=third


    The FOO=bar cmd form will really set FOO=bar in the environment of cmd, but a command like docker does not automatically export its own environment into the container, but the environment vars have to be added explicitly with the -e switch.



    Again, a more straightforward demo would be:



    FOO=before
    FOO=after env - FOO=$FOO printenv FOO
    => before





    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%2f520026%2fwhy-does-foo-bar-export-the-variable-into-my-environment%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









      4














      No, FOO=bar; does not export the variable into my environment



      A var is set in the (present) environment only if it was previously exported:



      $ export foo
      $ foo=bar
      $ env | grep foo
      foo=bar


      A variable is set in the environment of a command when it is placed before the command. Like foo=bar command. And it only exists while the command runs.



      $ foo=bar bash -c 'echo "foo is = $foo"'
      foo is = bar


      The var is not set for the command line (in the present shell):



      $ foo=bar bash -c echo $foo


      Above, the value of $foo is replaced with nothing by the present running shell, thus: no output.



      Your command:



      $ FOO=bar docker run -it -e FOO=$FOO debian env


      is converted to the actual string:



      $ FOO=bar docker run -it -e FOO= debian env


      by the present running shell.



      If, instead, you set the variable (in the present running shell) with foo=bar before running the command, the line will be converted to:



      $ FOO=bar; docker run -it -e FOO=bar debian env


      A variable set to the environment of a command is erased when the command returns:



      $ foo=bar bash -c 'echo'; echo "foo was erased: "$foo""


      Except when the command is a builtin in some conditions/shells:



      $ ksh -c 'foo=bar typeset baz=quuz; echo $foo'
      bar





      share|improve this answer




















      • 1





        The variable FOO is also not exported to the current shell environment, but set as a shell variable, unless it has been previously exported.

        – Johan Myréen
        2 hours ago











      • Not knowing anything about Docker, I wonder if it would actually work if the -e FOO=$FOO thing (which I presume is used to set an environment variable) is removed since FOO is set in Docker's environment.

        – Kusalananda
        2 hours ago












      • Not being fluent in docker language, I'll refrain from making any recomendation. But yes, what you comment seems reasonable. @Kusalananda

        – Isaac
        2 hours ago











      • @JohanMyréen Yes, Where I stated the contrary?

        – Isaac
        2 hours ago















      4














      No, FOO=bar; does not export the variable into my environment



      A var is set in the (present) environment only if it was previously exported:



      $ export foo
      $ foo=bar
      $ env | grep foo
      foo=bar


      A variable is set in the environment of a command when it is placed before the command. Like foo=bar command. And it only exists while the command runs.



      $ foo=bar bash -c 'echo "foo is = $foo"'
      foo is = bar


      The var is not set for the command line (in the present shell):



      $ foo=bar bash -c echo $foo


      Above, the value of $foo is replaced with nothing by the present running shell, thus: no output.



      Your command:



      $ FOO=bar docker run -it -e FOO=$FOO debian env


      is converted to the actual string:



      $ FOO=bar docker run -it -e FOO= debian env


      by the present running shell.



      If, instead, you set the variable (in the present running shell) with foo=bar before running the command, the line will be converted to:



      $ FOO=bar; docker run -it -e FOO=bar debian env


      A variable set to the environment of a command is erased when the command returns:



      $ foo=bar bash -c 'echo'; echo "foo was erased: "$foo""


      Except when the command is a builtin in some conditions/shells:



      $ ksh -c 'foo=bar typeset baz=quuz; echo $foo'
      bar





      share|improve this answer




















      • 1





        The variable FOO is also not exported to the current shell environment, but set as a shell variable, unless it has been previously exported.

        – Johan Myréen
        2 hours ago











      • Not knowing anything about Docker, I wonder if it would actually work if the -e FOO=$FOO thing (which I presume is used to set an environment variable) is removed since FOO is set in Docker's environment.

        – Kusalananda
        2 hours ago












      • Not being fluent in docker language, I'll refrain from making any recomendation. But yes, what you comment seems reasonable. @Kusalananda

        – Isaac
        2 hours ago











      • @JohanMyréen Yes, Where I stated the contrary?

        – Isaac
        2 hours ago













      4












      4








      4







      No, FOO=bar; does not export the variable into my environment



      A var is set in the (present) environment only if it was previously exported:



      $ export foo
      $ foo=bar
      $ env | grep foo
      foo=bar


      A variable is set in the environment of a command when it is placed before the command. Like foo=bar command. And it only exists while the command runs.



      $ foo=bar bash -c 'echo "foo is = $foo"'
      foo is = bar


      The var is not set for the command line (in the present shell):



      $ foo=bar bash -c echo $foo


      Above, the value of $foo is replaced with nothing by the present running shell, thus: no output.



      Your command:



      $ FOO=bar docker run -it -e FOO=$FOO debian env


      is converted to the actual string:



      $ FOO=bar docker run -it -e FOO= debian env


      by the present running shell.



      If, instead, you set the variable (in the present running shell) with foo=bar before running the command, the line will be converted to:



      $ FOO=bar; docker run -it -e FOO=bar debian env


      A variable set to the environment of a command is erased when the command returns:



      $ foo=bar bash -c 'echo'; echo "foo was erased: "$foo""


      Except when the command is a builtin in some conditions/shells:



      $ ksh -c 'foo=bar typeset baz=quuz; echo $foo'
      bar





      share|improve this answer















      No, FOO=bar; does not export the variable into my environment



      A var is set in the (present) environment only if it was previously exported:



      $ export foo
      $ foo=bar
      $ env | grep foo
      foo=bar


      A variable is set in the environment of a command when it is placed before the command. Like foo=bar command. And it only exists while the command runs.



      $ foo=bar bash -c 'echo "foo is = $foo"'
      foo is = bar


      The var is not set for the command line (in the present shell):



      $ foo=bar bash -c echo $foo


      Above, the value of $foo is replaced with nothing by the present running shell, thus: no output.



      Your command:



      $ FOO=bar docker run -it -e FOO=$FOO debian env


      is converted to the actual string:



      $ FOO=bar docker run -it -e FOO= debian env


      by the present running shell.



      If, instead, you set the variable (in the present running shell) with foo=bar before running the command, the line will be converted to:



      $ FOO=bar; docker run -it -e FOO=bar debian env


      A variable set to the environment of a command is erased when the command returns:



      $ foo=bar bash -c 'echo'; echo "foo was erased: "$foo""


      Except when the command is a builtin in some conditions/shells:



      $ ksh -c 'foo=bar typeset baz=quuz; echo $foo'
      bar






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 34 mins ago

























      answered 2 hours ago









      IsaacIsaac

      12.6k11956




      12.6k11956







      • 1





        The variable FOO is also not exported to the current shell environment, but set as a shell variable, unless it has been previously exported.

        – Johan Myréen
        2 hours ago











      • Not knowing anything about Docker, I wonder if it would actually work if the -e FOO=$FOO thing (which I presume is used to set an environment variable) is removed since FOO is set in Docker's environment.

        – Kusalananda
        2 hours ago












      • Not being fluent in docker language, I'll refrain from making any recomendation. But yes, what you comment seems reasonable. @Kusalananda

        – Isaac
        2 hours ago











      • @JohanMyréen Yes, Where I stated the contrary?

        – Isaac
        2 hours ago












      • 1





        The variable FOO is also not exported to the current shell environment, but set as a shell variable, unless it has been previously exported.

        – Johan Myréen
        2 hours ago











      • Not knowing anything about Docker, I wonder if it would actually work if the -e FOO=$FOO thing (which I presume is used to set an environment variable) is removed since FOO is set in Docker's environment.

        – Kusalananda
        2 hours ago












      • Not being fluent in docker language, I'll refrain from making any recomendation. But yes, what you comment seems reasonable. @Kusalananda

        – Isaac
        2 hours ago











      • @JohanMyréen Yes, Where I stated the contrary?

        – Isaac
        2 hours ago







      1




      1





      The variable FOO is also not exported to the current shell environment, but set as a shell variable, unless it has been previously exported.

      – Johan Myréen
      2 hours ago





      The variable FOO is also not exported to the current shell environment, but set as a shell variable, unless it has been previously exported.

      – Johan Myréen
      2 hours ago













      Not knowing anything about Docker, I wonder if it would actually work if the -e FOO=$FOO thing (which I presume is used to set an environment variable) is removed since FOO is set in Docker's environment.

      – Kusalananda
      2 hours ago






      Not knowing anything about Docker, I wonder if it would actually work if the -e FOO=$FOO thing (which I presume is used to set an environment variable) is removed since FOO is set in Docker's environment.

      – Kusalananda
      2 hours ago














      Not being fluent in docker language, I'll refrain from making any recomendation. But yes, what you comment seems reasonable. @Kusalananda

      – Isaac
      2 hours ago





      Not being fluent in docker language, I'll refrain from making any recomendation. But yes, what you comment seems reasonable. @Kusalananda

      – Isaac
      2 hours ago













      @JohanMyréen Yes, Where I stated the contrary?

      – Isaac
      2 hours ago





      @JohanMyréen Yes, Where I stated the contrary?

      – Isaac
      2 hours ago













      3














      There are a number of variations to consider:




      1. Just doing FOO=bar creates a variable named FOO with value bar, but that variable isn't passed along to new processes:



        $ echo $FOO
        $ FOO=bar
        $ echo $FOO
        bar
        $ bash # Start a new bash process
        $ echo $FOO
        # Variable is not set in the new process
        $ exit # Exit new bash process



      2. Running FOO=bar <command> will run the given command with the variable set (but doesn't affect the original shell's environment):



        $ echo $foo
        $ FOO=baz bash # start a new bash process
        $ echo $FOO
        baz
        $ exit # exit the new bash process
        exit
        $ echo $FOO
        # No FOO in the original bash process
        $



      3. Doing FOO=foo; <command> is equivalent to (1); putting a semicolon between two commands is equivalent to running those two commands on two separate lines:



        $ FOO=foo; echo $FOO
        foo
        $ bash
        $ echo $FOO

        $ exit
        exit
        $ echo $FOO
        foo
        $



      4. Using export will pass a variable in the shell's environment to newly-created processes:



        $ export FOO=bar
        $ echo $FOO # Value set in original shell
        bar
        $ bash # Start another shell
        $ echo $FOO
        bar # Value was passed along to new process
        $ exit
        exit






      share|improve this answer



























        3














        There are a number of variations to consider:




        1. Just doing FOO=bar creates a variable named FOO with value bar, but that variable isn't passed along to new processes:



          $ echo $FOO
          $ FOO=bar
          $ echo $FOO
          bar
          $ bash # Start a new bash process
          $ echo $FOO
          # Variable is not set in the new process
          $ exit # Exit new bash process



        2. Running FOO=bar <command> will run the given command with the variable set (but doesn't affect the original shell's environment):



          $ echo $foo
          $ FOO=baz bash # start a new bash process
          $ echo $FOO
          baz
          $ exit # exit the new bash process
          exit
          $ echo $FOO
          # No FOO in the original bash process
          $



        3. Doing FOO=foo; <command> is equivalent to (1); putting a semicolon between two commands is equivalent to running those two commands on two separate lines:



          $ FOO=foo; echo $FOO
          foo
          $ bash
          $ echo $FOO

          $ exit
          exit
          $ echo $FOO
          foo
          $



        4. Using export will pass a variable in the shell's environment to newly-created processes:



          $ export FOO=bar
          $ echo $FOO # Value set in original shell
          bar
          $ bash # Start another shell
          $ echo $FOO
          bar # Value was passed along to new process
          $ exit
          exit






        share|improve this answer

























          3












          3








          3







          There are a number of variations to consider:




          1. Just doing FOO=bar creates a variable named FOO with value bar, but that variable isn't passed along to new processes:



            $ echo $FOO
            $ FOO=bar
            $ echo $FOO
            bar
            $ bash # Start a new bash process
            $ echo $FOO
            # Variable is not set in the new process
            $ exit # Exit new bash process



          2. Running FOO=bar <command> will run the given command with the variable set (but doesn't affect the original shell's environment):



            $ echo $foo
            $ FOO=baz bash # start a new bash process
            $ echo $FOO
            baz
            $ exit # exit the new bash process
            exit
            $ echo $FOO
            # No FOO in the original bash process
            $



          3. Doing FOO=foo; <command> is equivalent to (1); putting a semicolon between two commands is equivalent to running those two commands on two separate lines:



            $ FOO=foo; echo $FOO
            foo
            $ bash
            $ echo $FOO

            $ exit
            exit
            $ echo $FOO
            foo
            $



          4. Using export will pass a variable in the shell's environment to newly-created processes:



            $ export FOO=bar
            $ echo $FOO # Value set in original shell
            bar
            $ bash # Start another shell
            $ echo $FOO
            bar # Value was passed along to new process
            $ exit
            exit






          share|improve this answer













          There are a number of variations to consider:




          1. Just doing FOO=bar creates a variable named FOO with value bar, but that variable isn't passed along to new processes:



            $ echo $FOO
            $ FOO=bar
            $ echo $FOO
            bar
            $ bash # Start a new bash process
            $ echo $FOO
            # Variable is not set in the new process
            $ exit # Exit new bash process



          2. Running FOO=bar <command> will run the given command with the variable set (but doesn't affect the original shell's environment):



            $ echo $foo
            $ FOO=baz bash # start a new bash process
            $ echo $FOO
            baz
            $ exit # exit the new bash process
            exit
            $ echo $FOO
            # No FOO in the original bash process
            $



          3. Doing FOO=foo; <command> is equivalent to (1); putting a semicolon between two commands is equivalent to running those two commands on two separate lines:



            $ FOO=foo; echo $FOO
            foo
            $ bash
            $ echo $FOO

            $ exit
            exit
            $ echo $FOO
            foo
            $



          4. Using export will pass a variable in the shell's environment to newly-created processes:



            $ export FOO=bar
            $ echo $FOO # Value set in original shell
            bar
            $ bash # Start another shell
            $ echo $FOO
            bar # Value was passed along to new process
            $ exit
            exit







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 hours ago









          Andy DaltonAndy Dalton

          5,3091823




          5,3091823





















              0















              FOO=bar docker run -it -e FOO=$FOO debian env




              Here the $FOO from FOO=$FOO will be expanded before the FOO=bar assignment happens.



              You can check that with a more straight-forward example:



              FOO=first
              FOO=second echo FOO=$FOO
              => FOO=first

              FOO=third; echo FOO=$FOO
              => FOO=third


              The FOO=bar cmd form will really set FOO=bar in the environment of cmd, but a command like docker does not automatically export its own environment into the container, but the environment vars have to be added explicitly with the -e switch.



              Again, a more straightforward demo would be:



              FOO=before
              FOO=after env - FOO=$FOO printenv FOO
              => before





              share|improve this answer





























                0















                FOO=bar docker run -it -e FOO=$FOO debian env




                Here the $FOO from FOO=$FOO will be expanded before the FOO=bar assignment happens.



                You can check that with a more straight-forward example:



                FOO=first
                FOO=second echo FOO=$FOO
                => FOO=first

                FOO=third; echo FOO=$FOO
                => FOO=third


                The FOO=bar cmd form will really set FOO=bar in the environment of cmd, but a command like docker does not automatically export its own environment into the container, but the environment vars have to be added explicitly with the -e switch.



                Again, a more straightforward demo would be:



                FOO=before
                FOO=after env - FOO=$FOO printenv FOO
                => before





                share|improve this answer



























                  0












                  0








                  0








                  FOO=bar docker run -it -e FOO=$FOO debian env




                  Here the $FOO from FOO=$FOO will be expanded before the FOO=bar assignment happens.



                  You can check that with a more straight-forward example:



                  FOO=first
                  FOO=second echo FOO=$FOO
                  => FOO=first

                  FOO=third; echo FOO=$FOO
                  => FOO=third


                  The FOO=bar cmd form will really set FOO=bar in the environment of cmd, but a command like docker does not automatically export its own environment into the container, but the environment vars have to be added explicitly with the -e switch.



                  Again, a more straightforward demo would be:



                  FOO=before
                  FOO=after env - FOO=$FOO printenv FOO
                  => before





                  share|improve this answer
















                  FOO=bar docker run -it -e FOO=$FOO debian env




                  Here the $FOO from FOO=$FOO will be expanded before the FOO=bar assignment happens.



                  You can check that with a more straight-forward example:



                  FOO=first
                  FOO=second echo FOO=$FOO
                  => FOO=first

                  FOO=third; echo FOO=$FOO
                  => FOO=third


                  The FOO=bar cmd form will really set FOO=bar in the environment of cmd, but a command like docker does not automatically export its own environment into the container, but the environment vars have to be added explicitly with the -e switch.



                  Again, a more straightforward demo would be:



                  FOO=before
                  FOO=after env - FOO=$FOO printenv FOO
                  => before






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 1 hour ago

























                  answered 1 hour ago









                  pizdelectpizdelect

                  1,150211




                  1,150211



























                      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.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f520026%2fwhy-does-foo-bar-export-the-variable-into-my-environment%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

                      Log på Navigationsmenu

                      Wonderful Copenhagen (sang) Eksterne henvisninger | NavigationsmenurSide på frankloesser.comWonderful Copenhagen

                      Detroit Tigers Spis treści Historia | Skład zespołu | Sukcesy | Członkowie Baseball Hall of Fame | Zastrzeżone numery | Przypisy | Menu nawigacyjneEncyclopedia of Detroit - Detroit TigersTigers Stadium, Detroit, MITigers Timeline 1900sDetroit Tigers Team History & EncyclopediaTigers Timeline 1910s1935 World Series1945 World Series1945 World Series1984 World SeriesComerica Park, Detroit, MI2006 World Series2012 World SeriesDetroit Tigers 40-Man RosterDetroit Tigers Coaching StaffTigers Hall of FamersTigers Retired Numberse