Skip to main content
Tweeted twitter.com/StackUnix/status/1144530951655088128
Rollback to Revision 2 - Edit approval overridden by post owner or moderator
Link
Justin Garrison
  • 7.5k
  • 4
  • 22
  • 21

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

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

Became Hot Network Question
added example with `docker`
Source Link
Justin Garrison
  • 7.5k
  • 4
  • 22
  • 21

If I run

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

That environment variable is not persistedset in my environmentthe command output for later commandsthe env command.

echo $FOOPATH=/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 always available until I unset FOOfrom the container and also exported into my current shell environment.

echo $FOO
bar

unset FOO
echo $FOO
  

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

If I run

FOO=bar

That environment variable is not persisted in my environment for later commands.

echo $FOO
 

But if I run

FOO=bar;

Then the variable is always available until I unset FOO

echo $FOO
bar

unset FOO
echo $FOO
  

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

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?

Source Link
Justin Garrison
  • 7.5k
  • 4
  • 22
  • 21
Loading