12

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?

4 Answers 4

20

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.

4
  • What means "in the current environment"? Current bash script or?
    – George2
    May 21, 2010 at 5:59
  • 1
    Yes, 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. May 21, 2010 at 8:07
  • 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
    May 21, 2010 at 16:27
  • 1
    @George2: See my edited answer for more information. May 21, 2010 at 17:13
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

6
  • 2
    You might want to reread the section of the Bash man page regarding when those files are sourced. May 20, 2010 at 21:51
  • 1
    Yup... I got login and interactive confused. Editing my answer to reflect. Thanks for the correction!
    – whaley
    May 20, 2010 at 21:54
  • "make an environmental variable persists" -- what means persists?
    – George2
    May 21, 2010 at 6:19
  • 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. May 21, 2010 at 8:12
  • 1
    That means that you have not made it persistent. Try doing what whaley described. May 21, 2010 at 17:09
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
7
  • 2
    Bourne shell requires VARIABLE=value; export VARIABLE
    – mpez0
    May 20, 2010 at 18:44
  • 1
    This should work too...
    – Pylsa
    May 20, 2010 at 18:48
  • @BloodPhilia, for bash, only one way to set environment variable?
    – George2
    May 21, 2010 at 6:21
  • 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.
    – Pylsa
    May 21, 2010 at 9:13
  • 1
    executethisprogram 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. May 21, 2010 at 17:13
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
4
  • I am confused about "VAR=val application", what means val and what means application?
    – George2
    May 21, 2010 at 6:23
  • 2
    These 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. May 21, 2010 at 8:09
  • ls is child environment of what? I am confused.
    – George2
    May 21, 2010 at 16:33
  • 1
    ls 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. May 21, 2010 at 17:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.