Pages

Friday, August 24, 2012

Linux Environment Variable


An environment variable is a named object that contains data used by one or more applications. In simple terms, it is a variable with a name and a value. The value of an environmental variable can for example be the location of all executable files in the file system, the default editor that should be used, or the system locale settings. Environment variables in Linux are used by most of the activities taking place on a Linux system.

To view all the env variables as shown below ,we can use ‘export -p ‘ which gives

declare -x COLORTERM="gnome-terminal"
declare -x CVS_RSH="ssh"
declare -x DBUS_SESSION_BUS_ADDRESS="unix:abstract=/tmp/dbus-S3krTO7dZY,guid=b162193da5fe51c03cd49000501ffa05"
declare -x DESKTOP_SESSION="default"
declare -x DISPLAY=":0.0"
declare -x GDMSESSION="default"
declare -x GDM_XSERVER_LOCATION="local"
declare -x GNOME_DESKTOP_SESSION_ID="Default"
declare -x GNOME_KEYRING_SOCKET="/tmp/keyring-n8xiLd/socket"
declare -x GTK_RC_FILES="/etc/gtk/gtkrc:/root/.gtkrc-1.2-gnome2"
declare -x G_BROKEN_FILENAMES="1"
declare -x HISTSIZE="1000"

The same thing can be achieved by ‘env’ or ‘set’ or 'printenv' commands.

View a Specific Exported Variable

[root@vx111a ~]# echo $PATH
/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:

Set an Environment Variable: To set a environment variable ,we can use the export command like ,
[root@vx111a ~]# export $TEMP=tmp

Once the variable is set we can view that using,

[root@vx111a ~]# echo $TEMP
Tmp

We can also use ,  env EDIT=VIM to set the environment variable, This will set the editor to vim.

Remove a Environment Variable: To set a environment variable, we can use the unset command like [root@vx111a ~]# unset TEM

Append a Value to an Environment Variable: Linux allows us to append to the existing variables like,

export PATH=$PATH:/usr/jdk1.6.0_14/bin;

Exporting Variables Permanently: To export variables permanently, you can add the export command in any of the following start-up files :

~/.profile
~/.bash_profile
/etc/profile

Happy Learning