You are not logged in.

#1 2008-08-09 20:53:30

void.pointer
Member
From: Dallas, TX
Registered: 2008-07-30
Posts: 239

[SOLVED] What is export?

Hi,

I've read the man pages on 'export', however it still isn't making sense as to what its purpose is. What would be the difference between these two lines of script:

PATH=/usr/local/bin:$PATH

-vs-

export PATH=/usr/local/bin:$PATH



Thanks for reading.

Last edited by void.pointer (2008-08-09 21:33:58)

Offline

#2 2008-08-09 21:25:33

ghostHack
Member
From: Bristol UK
Registered: 2008-02-29
Posts: 261

Re: [SOLVED] What is export?

Basically 'export'ing a variable makes it available to sub-process you may start, you can pass variables through layers in this way.

From the Bash man page

export

              export [-fn] [-p] [name[=value]]

    Mark each name to be passed to child processes in the environment. If the -f option is supplied, the names refer to shell functions; otherwise the names refer to shell variables. The -n option means to no longer mark each name for export. If no names are supplied, or if the -p option is given, a list of exported names is displayed. The -p option displays output in a form that may be reused as input. If a variable name is followed by =value, the value of the variable is set to value.

    The return status is zero unless an invalid option is supplied, one of the names is not a valid shell variable name, or -f is supplied with a name that is not a shell function.

If you want a concrete example take /etc/profile, this script is executed by the login shell and defines things like the PATH, locale etc.  These are exported so that sub-processes of your login shell, i.e. everything else you run while you're logged in knows what PATH to look in for executables, what locale to use for output etc.

Last edited by ghostHack (2008-08-09 21:27:42)

Offline

#3 2008-08-09 21:26:05

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Re: [SOLVED] What is export?

I always treated exporting as making a variable global.

Offline

#4 2008-08-09 21:27:17

void.pointer
Member
From: Dallas, TX
Registered: 2008-07-30
Posts: 239

Re: [SOLVED] What is export?

What is a sub-process or child-process?

Offline

#5 2008-08-09 21:31:23

ghostHack
Member
From: Bristol UK
Registered: 2008-02-29
Posts: 261

Re: [SOLVED] What is export?

void.pointer wrote:

What is a sub-process or child-process?

lets say you have a script called do_stuff.sh and another called more_stuff.sh.  If do_stuff.sh is

#!/bin/sh

DIR=/home/somedir

export ARGS="print"

more_stuff.sh

more_stuff.sh would be a sub-process of do_stuff.sh and would be able to use the $ARGS variable (which would contain "print") directly because it was exported.  However more_stuff.sh would not know about $DIR.

Offline

#6 2008-08-09 21:33:39

lucke
Member
From: Poland
Registered: 2004-11-30
Posts: 4,018

Offline

#7 2008-08-09 21:33:44

void.pointer
Member
From: Dallas, TX
Registered: 2008-07-30
Posts: 239

Re: [SOLVED] What is export?

I see what you mean now. So export has no affect outside of a script, assuming someone doesn't create a script on a single command at the prompt. Thanks for the help guys. Marking as solved.

Offline

#8 2008-08-10 08:31:06

robertp
Member
From: Warszawa, Poland
Registered: 2007-09-11
Posts: 123

Re: [SOLVED] What is export?

Export affects not only shell (e.g. bash) scripts but also compiled programs and scripts in other languages (Python, Perl). Exporting variable in bash marks it as exportable. When you run any program (compiled, shell script or other language script) all such exportable variables will be copied into environment of child-process. Child-process may at any moment access its environment using $VAR construction in bash, getenv(varname) in C language or any other construction provided by programming language.

Variables which were not exported at the time of starting child-process are not copied into enviroment of child-process and are not available in it.

I hope this should explain everything.

Offline

Board footer

Powered by FluxBB