Difference between revisions of "Environment variable"

From Linuxintro
imported>ThorstenStaerk
(Created page with "An environment variable holds information in your shell session. For example, take the often-used variable PATH that tells the shell where to look for executables: $...")
 
imported>ThorstenStaerk
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
 
  /sbin:/usr/sbin:/usr/local/sbin:/opt/kde3/sbin:/opt/gnome/sbin:/root/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/usr/NX/bin:/usr/lib/qt3/bin
 
  /sbin:/usr/sbin:/usr/local/sbin:/opt/kde3/sbin:/opt/gnome/sbin:/root/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/usr/NX/bin:/usr/lib/qt3/bin
  
You can list the current [[environment variable]]s in a [[console]] like this:
+
You can list the current [[environment variable]]s by [[opening a console]] and entering the command:
  $ [[env]]
+
  env
  
 
= Set an environment variable =
 
= Set an environment variable =
 
You can set a variable like this:
 
You can set a variable like this:
 
 
  $ export MYVARIABLE=1000
 
  $ export MYVARIABLE=1000
 
  $ echo "I just defined $MYVARIABLE"
 
  $ echo "I just defined $MYVARIABLE"
Line 18: Line 17:
 
  export myvar="this is set"
 
  export myvar="this is set"
 
  echo $myvar
 
  echo $myvar
  $ [[sh]] myscript.sh
+
  $ [http://en.wikipedia.org/wiki/Bourne_shell sh] myscript.sh
 
  this is set
 
  this is set
 
  $ echo $myvar
 
  $ echo $myvar

Latest revision as of 03:38, 23 January 2012

An environment variable holds information in your shell session. For example, take the often-used variable PATH that tells the shell where to look for executables:

$ echo $PATH
/sbin:/usr/sbin:/usr/local/sbin:/opt/kde3/sbin:/opt/gnome/sbin:/root/bin:/usr/local/bin:/usr/bin:/usr/X11R6/bin:/bin:/usr/games:/opt/gnome/bin:/opt/kde3/bin:/usr/lib/jvm/jre/bin:/usr/lib/mit/bin:/usr/lib/mit/sbin:/usr/NX/bin:/usr/lib/qt3/bin

You can list the current environment variables by opening a console and entering the command:

env

Set an environment variable

You can set a variable like this:

$ export MYVARIABLE=1000
$ echo "I just defined $MYVARIABLE"
I just defined 1000

Environment variables are not global, they are maintained separately per process. A process may change its own, but cannot affect the environment variables of other processes. When a process forks its children inherit the parent's environment variables. That means that you cannot change a variable by calling a program:

$ cat >myscript.sh
#!/bin/bash
export myvar="this is set"
echo $myvar
$ sh myscript.sh
this is set
$ echo $myvar

$            

In order to set variables from a script, you have to source the script:

$ source myscript.sh
this is set
$ echo $myvar
this is set

Note that sourcing can also be done with a dot:

$ export myvar=empty
$ . myscript.sh
this is set
$ echo $myvar
this is set

User-specific variables are set in the $HOME/.bash_profile or $HOME/.bashrc files as these are the files that are sourced when you log in. These variables are available throughout the shell session unless you re-define them.

Process environments

You can see the environment variables of every process with the command:

cat /proc/12345/environ

Where 12345 is the process' PID.

See also