Setting Linux environment variables

9

5

I am running Red Hat Linux Enterprise 5; I am always using the export command to set environment variables.

Are there any other ways to set environment variables and what are the advantages/disadvantages of them?

George2

Posted 2010-05-20T17:08:34.470

Reputation: 4 239

Answers

16

This is an excerpt from the Bash man page:

export [-fn] [name[=word]] ...
export -p
The supplied names are marked for automatic export to the environment of subsequently executed commands. If the -f option is given, the names refer to functions...

If you only need the variable in the current environment, it's not necessary to use export.

var=value

Edit:

Without export: current environment only. With export: current environment and child environments.

Here's a demonstration of the affect of export on availability of a variable in a child environment and that changes in the child environment don't affect the parent:

$ var1=123
$ export var2=456
$ echo "parent [$var1] [$var2] [$var3]"
parent [123] [456] []
$ var3=789 bash -c 'echo "child [$var1] [$var2] [$var3]"; var1=111; var2=222; var3=333; echo "child [$var1] [$var2] [$var3]"'
child [] [456] [789]
child [111] [222] [333]
$ echo "parent [$var1] [$var2] [$var3]"
parent [123] [456] []

After the first echo (echo "parent...") you see "123" and "456" because both var1 and var2 are active in the current environment. You don't see a value for var3 because it's not set yet.

After the line that starts "var3=..." you don't see a value for var1 because it wasn't exported. You do see a value for var2 because it was exported. You see a value for var3 because it was set for the child environment only.

(bash -c is equivalent to running a script with the contents of the argument to the -c option. A script or other executable or, in this case, the argument to bash -c becomes a child of the current environment which, as a result is, of course, the child's parent.)

In the "script" the values of the variable are changed. It now outputs those new values.

Once the "script" is finished, execution returns to the parent environment (the command line in this case). After the last echo, you see the original values because the changes made in the child environment do not affect the parent.

Paused until further notice.

Posted 2010-05-20T17:08:34.470

Reputation: 86 075

What means "in the current environment"? Current bash script or? – George2 – 2010-05-21T05:59:52.927

1Yes, it means "in the current script" or "in the current interactive session". So that excludes executables or scripts that are run from within the current script or interactive session. That's the purpose of export - to make variables available to these child environments. – Paused until further notice. – 2010-05-21T08:07:55.687

I find some conflicts from what you said. :-)

You said "So that excludes executables or scripts", pay attention to "exclude", but in what you quoted, you mentioned "The supplied names are marked for automatic export to the environment of subsequently executed commands." -- it to be "include" (automatic exported to, or impact), and not "exclude". I am confused, could you clarify please? – George2 – 2010-05-21T16:27:34.777

1@George2: See my edited answer for more information. – Paused until further notice. – 2010-05-21T17:13:55.070

3

You say that

I am always using export command to set environment variable

By the way you worded that, it sounds like you are really trying to ask how do you make an environmental variable persists. To do that would require you to place your export VAR="foo" statement in your $HOME/.bash_profile file (if you are using bash). If you want that environmental variable to persist for all users but root, then add it to /etc/profile. If you want it added for the root user too, then set it in /root/.bash_profile .

This will work for all login shells where bash is the shell of choice. For non login shells, you need to use .bashrc. I have no insights to offer for other shells :D

whaley

Posted 2010-05-20T17:08:34.470

Reputation: 1 376

2You might want to reread the section of the Bash man page regarding when those files are sourced. – Paused until further notice. – 2010-05-20T21:51:14.883

1Yup... I got login and interactive confused. Editing my answer to reflect. Thanks for the correction! – whaley – 2010-05-20T21:54:14.873

"make an environmental variable persists" -- what means persists? – George2 – 2010-05-21T06:19:01.633

1"Persist" means that you want this variable to have this value each time a shell is started. It may or may not be necessary to export the variable depending on what its purpose is. – Paused until further notice. – 2010-05-21T08:12:20.763

I have tried that if I open one terminal window and export a variable, then open another terminal window, the variable is not set. I think it is not persistent, any comments? – George2 – 2010-05-21T16:24:09.090

1That means that you have not made it persistent. Try doing what whaley described. – Paused until further notice. – 2010-05-21T17:09:23.863

1

export is the most straightforward way to do it, so why not leave it at that?

export VARIABLE=value    # for Bourne, bash, and similar shells
setenv VARIABLE value    # for csh and similar shells

BloodPhilia

Posted 2010-05-20T17:08:34.470

Reputation: 27 374

2Bourne shell requires VARIABLE=value; export VARIABLE – mpez0 – 2010-05-20T18:44:37.280

1This should work too... – BloodPhilia – 2010-05-20T18:48:12.413

@BloodPhilia, for bash, only one way to set environment variable? – George2 – 2010-05-21T06:21:33.093

@mpez0, what are the differences between using export and using =? – George2 – 2010-05-21T06:22:17.693

1@George2 - When using VARIABLE=value, you're creating a local variable which is gone after current script execution. When using export, children processes inherit the variable values from their parent process. For example: FOO=BAR; executethisprogram

The program executethisprogram WILL NOT know the value of variable FOO. On the other hand, when using: FOO=BAR; export FOO; executethisprogram

The program executethisprogram WILL know the value of variable FOO. – BloodPhilia – 2010-05-21T09:13:16.437

in your sample "FOO=BAR; export FOO; executethisprogram", why executethisprogram is a subprocess of "export FOO" (please let me know if I am wrongly understand your points)? I think executethisprogram and "export FOO" should be two sequentially executed program and parallel relationship, why it is parent-child relationship? – George2 – 2010-05-21T16:32:41.707

1executethisprogram is not a subprocess of "export FOO". It's a subprocess (child) of the script that calls it or the interactive (command-line) shell. export FOO is a command that marks the variable FOO so that it can be accessed by child processes of the envrionment in which the command was issued. That parent may be a script or an interactive shell. – Paused until further notice. – 2010-05-21T17:13:28.970

1

You can also do something like this:

VAR=val application

For example:

LANG=C ls --help

output in English.

LANG=pl_PL ls --help

output in Polish (if avaliable).

In the past in sh you couldn't do export VAL=val. You had to

VAL=val; export VAL

gogiel

Posted 2010-05-20T17:08:34.470

Reputation: 300

I am confused about "VAR=val application", what means val and what means application? – George2 – 2010-05-21T06:23:21.777

2These examples set the value of the variable for the child environment (ls, for example) without affecting the value of that variable in the current (parent) environment. – Paused until further notice. – 2010-05-21T08:09:54.730

ls is child environment of what? I am confused. – George2 – 2010-05-21T16:33:35.680

1ls is a child of the process from which it was run. If I type ls at a command prompt, the interactive shell is the parent and ls is the child. If I have a script that uses ls then the script is the parent and ls is the child. – Paused until further notice. – 2010-05-21T17:15:58.637