what does export do?

Deron Meranda deron.meranda at gmail.com
Mon May 23 02:04:31 UTC 2005


> so, when a command is entered it winds its way "up" (or down) until a
> match is found.  hmm, think must I on this.

Environment variables are private to each process.  Although each
process *usually* gets its initial set of variables by copying those
from it's parent, from that point on they are different.  A variable
lookup does not go "up the chain" of processes...it only looks in the
current process.

You can see the current set of environment variables in a shell by
using the "env" builtin command.  For non-shell programs there is
usually a language-dependent method to get to them.  In C/C++ for
instance there is the environ[] array as well as the getenv(3) library
call.  In Python you have the sys.environ dictionary.  And so on for
other languages.

If you want to see the environment variables in some other process
your choices are more restricted.  Given that you have proper security
permissions, you can look at the /proc/nnnn/environ file, where nnnn
is the process ID number.  The variables are separated by 0-valued
bytes, so the easiest way to just peek at them is perhaps,

  cat /proc/12345/environ | xargs -0 -n 1 echo     # that's a
digit-zero in the "-0"

They are usually listed unsorted, so you can also pipe it through the
sort command.
[The above technique is Linux-specific; other Unix-based OS's have
slightly different mechanisms].

Now, as far as the export command...the shell (bash and others) is
really just a simple interactive programming language.  Like most
programming languages it has it's own concept of a "variable".  The
shell's variables are actually distinct from environment variables,
which the kernel itself keeps.  The export command tells the shell
that you want it to essentially equate the two....meaning that
whenever you change the value of a shell variable, it will also
implicitly change the value of a equivalently named environment
variable.  Environment variables will be inherited by any child
processes the shell happens to start, shell variables will not be.

Incidentally you only need to export a variable once; you don't need
to keep exporting it every time you change it's value.
-- 
Deron Meranda




More information about the users mailing list