Re: what does export do?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



linux user wrote:

On Monday 23 May 2005 02:04, Deron Meranda wrote:

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

This is by far the most complete and understandable explanation of environment variables and exporting. Not just answers the question, but also lets one learn the concept behind it. I just wish that there be as many as possible such good answers on the list... :-))


Yes.  It is very detailed.  Here are a couple of minor details to add to what's above.

If a user exports an environment variable from a terminal it's only good for that terminal.

linux@example% export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:lib

The above line sets the environment variable LD_LIBRARY_PATH to the current LD_LIBRARY_PATH plus the relative lib directory.

If you're using the bash shell, to view an entire list of environment variables for a terminal just type "export".  To make permanent changes to environment variables, again if using the bash shell, the user can go to their home location and edit the .bashrc file.

.bashrc example:

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:home/linux/lib

After adding the above line to the .bashrc file, the load library path will be the same for every terminal and doesn't have to be set each time a new terminal is opened.

If you want this path to be available to any terminal that is open you will have to "source .bashrc" at the command prompt.  Otherwise, just close all active terminals and open new ones.


Errm don't you need to export that ie;
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:home/linux/lib
export LD_LIBRARY_PATH

Regards.

Rick

--
Email which contains HTML will be deleted unread.


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux