How To Set Environment Variables In UNIX And Linux


UNIX

How Environment Variables are set and used varies with the shell.

# env 
List all environment variables and their values

# env $HOME
/root

HOME is an environment and variable /root is value

# setenv VAR_NAME value
Set new environment variable.

# unsetenv VAR_NUM
Undefine existing environment variable.


Linux

# set
List all environment variables and their value.

# echo $HOME
Get value of environment variable.

# export NEW_VARIABLE=value
Set new variable with its value

# export PATH=${PATH}:/usr/app/application-x.x.x/bin
OR
# export MY_DIR=/local/dir
# export PATH=${PATH}:${MY_DIR}
(Add) Append new value in existing variable

All above settings are temporary or for opened terminal only.


To make Changes Permenant

Edit file .bash_profile using,
# vi ~/.bash_profile 

.bash_profile file already contains few script lines for setting PATH variable. 
If you wish to add new PATH value just append it to existing line using " : " (colon).
PATH=$PATH:$HOME/bin:/my/new/app/path 

OR If you wish to set new environment variable just add following lines at the end of file. NEW_VARIABLE=/my/app/path
export NEW_VARIABLE
 

Save and close the file.

Note:
.bash_profile file is get executed at the time of system startup or user logged in. So to watch your environment variable running (or removed) restrt or re-logging your system.


No comments :

Post a Comment