7

And why is an export needed? Where is it exporting to?

nonopolarity
  • 146,324
  • 131
  • 460
  • 740
  • possible duplicate of [bash: defining a variable with or without export](http://stackoverflow.com/questions/1158091/bash-defining-a-variable-with-or-without-export) – Dennis Williamson Aug 05 '10 at 05:48
  • I'm still wondering about "where is it exporting to." Is it being saved somewhere permanently? – punstress Sep 24 '13 at 03:01
  • If I just type the command `export` alone in Cygwin, I get a big list of env vars. So I exported one I needed for a program and then typed `export` again and sure enough it showed up there. However, it did not show up when I looked under My Computer > Advanced > Environment Variables (Win XP), and I checked both User and System. I kind of expected it to be there, so I'm a little stumped, because those env vars and others showed up in the big list. If it had been there, I could have clicked Delete. I wonder where it is ... ? – punstress Sep 25 '13 at 09:36

2 Answers2

7

Exported variables are passed on to new processes invoked.

Try setting A=1, then invoking a new shell by entering "bash", then echo $A - an empty line.

Do the same, but then export A=1, invoke a new shell, then echo $A - voila!

edit on the technical side, and looking at your question, B=1 doesn't actually set an environment variable. To get the real environment of your shell (in linux), try

$ xargs -n 1 -0 echo < /proc/$$/environ

which differs from the output of export. And as a sidenote, this question touches on the internals of bash and its environment handling.

Community
  • 1
  • 1
mvds
  • 45,755
  • 8
  • 102
  • 111
1

The PS1 environment variable is pre-defined by the bash shell; consequently, it doesn't need to be exported, merely set.

Steve Emmerson
  • 7,702
  • 5
  • 33
  • 59
  • Nope, it doesn't need to be exported, because the only one needing it is bash itself, and not it's (grand)children. – mvds Aug 05 '10 at 02:25
  • 1
    @mvds No, it doesn't need to be exported because it's already an environment variable. – Steve Emmerson Aug 05 '10 at 02:59
  • 2
    3 checks say that's not true (by default in bash 3.2.39) : 1) `export |grep PS1` 2) `xargs -n 1 -0 echo < /proc/$$/environ` and 3) `echo "main(int i,char**v,char**e){while(*e)printf(\"%s\n\",*e++);}" | gcc -x c - && ./a.out |grep PS1`. `set` however *does* show PS1. Note that all of these methods show `PATH` which I believe is a true "environment variable" (don't know your exact definition btw) – mvds Aug 05 '10 at 10:28