You are not logged in.
Pages: 1
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
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
I always treated exporting as making a variable global.
Offline
What is a sub-process or child-process?
Offline
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
Offline
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
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
Pages: 1