Linux: Set Environment Variable

by on April 14, 2011 · 19 comments· LAST UPDATED March 29, 2012

in

Bash shell is used for various purposes under Linux. How do I customize the shell environment variable under Linux operating systems?

You can use shell variables to store data, set configuration options and customize the shell environment under Linux. The default shell is Bash under Linux and can be used for the following purposes:

  1. Configure look and feel of shell.
  2. Setup terminal settings depending on which terminal you're using.
  3. Set the search path such as JAVA_HOME, and ORACLE_HOME.
  4. Set environment variables as needed by programs.
  5. Run commands that you want to run whenever you log in or log out.
  6. Setup aliases and/or shell function to automate tasks to save typing and time.
  7. Changing bash prompt.
  8. Setting shell options.

You can use the following commands to view and configure the environment.

Display Current Environment

Type the following command:
$ set
Sample outputs:

BASH=/bin/bash
BASH_ARGC=()
BASH_ARGV=()
BASH_LINENO=()
BASH_SOURCE=()
BASH_VERSINFO=([0]="3" [1]="2" [2]="25" [3]="1" [4]="release" [5]="x86_64-redhat-linux-gnu")
BASH_VERSION='3.2.25(1)-release'
COLORS=/etc/DIR_COLORS.xterm
COLUMNS=237
CVS_RSH=ssh
DIRSTACK=()
EUID=0
GROUPS=()
G_BROKEN_FILENAMES=1
HISTFILE=/root/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000
HOME=/root
HOSTNAME=server3.www.p.cyberciti.biz
HOSTTYPE=x86_64
IFS=$' \t\n'
INPUTRC=/etc/inputrc
LANG=en_US.UTF-8
LESSOPEN='|/usr/bin/lesspipe.sh %s'
LINES=64
LOGNAME=root
LS_COLORS='no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:'
MACHTYPE=x86_64-redhat-linux-gnu
MAIL=/var/spool/mail/root
MAILCHECK=60
OPTERR=1
OPTIND=1
OSTYPE=linux-gnu
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
PIPESTATUS=([0]="0")
PPID=35469
PROMPT_COMMAND='echo -ne "\033]0;${USER}@${HOSTNAME%%.*}:${PWD/#$HOME/~}"; echo -ne "\007"'
PS1='[\u@\h \W]\$ '
PS2='> '
PS4='+ '
PWD=/root
SHELL=/bin/bash
SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
SHLVL=1
SSH_CLIENT='10.1.3.116 44212 22'
SSH_CONNECTION='10.1.3.116 44212 10.10.29.68 22'
SSH_TTY=/dev/pts/0
TERM=xterm
UID=0
USER=root
_=set
consoletype=pty
tmpid=0
genpasswd ()
{
    local l=$1;
    [ "$l" == "" ] && l=16;
    tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}
xrpm ()
{
    [ "$1" != "" ] && ( rpm2cpio "$1" | cpio -idmv )
}

The $PATH defined the search path for commands. It is a colon-separated list of directories in which the shell looks for commands. The $PS1 defines your prompt settings. See the list of all commonly used shell variables for more information. You can display the value of a variable using printf or echo command:
$ echo "$HOME"
OR
$ printf "%s\n" $HOME
Sample outputs:
/home/vivek

Task: Set Environment Variables on Linux

You can modify each environmental or system variable using the export command. Set the PATH environment variable to include the directory where you installed the bin directory with perl and shell scripts:

 
export PATH=${PATH}:/home/vivek/bin
 

OR

 
export PATH=${PATH}:${HOME}/bin
 

To set the JAVA_HOME environment variable to the directory where you installed the J2SE SDK application, enter:

 
export PATH=${PATH}:/usr/java/jdk1.5.0_07/bin
 

You can set multiple paths as follows:

 
export ANT_HOME=/path/to/ant/dir
export PATH=${PATH}:${ANT_HOME}/bin:${JAVA_HOME}/bin
 

How Do I Make All Settings permanent?

The ~/.bash_profile ($HOME/.bash_profile) or ~/.prfile file is executed when you login using console or remotely using ssh. Type the following command to edit ~/.bash_profile file, enter:
$ vi ~/.bash_proflle
Append the $PATH settings, enter:
export PATH=${PATH}:${HOME}/bin
Save and close the file.

Set IBM DB2 Instance Name

Type the following command:

 
export DB2INSTANCE=prod_sales
 

A Note About /etc/profile File

/etc/profile contains Linux system wide environment and startup programs. It is used by all users with bash, ksh, sh shell. Usually used to set PATH variable, user limits, and other settings for user. It only runs for login shell. If you wanted to make large changes or application specific changes use /etc/profile.d/ directory as explained here and here.

See also:

TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 19 comments… read them below or add one }

1 ./Amit.sh April 15, 2011 at 9:55 am

Setting en variable
—————————-
1) If u want to set the environment variables which will be global to all the users on the Linux box:
then u can modify the /etc/profile file
To bring the change u made to it in effect: do either of these -
a) source /etc/profile
b) su – root
c) Relogin to the machine

2) If you want the environment variables to be set only for your user:
then modify the ~/.bash_profile file and set the env. variables there.

To bring the change u made to it in effect: do either of these -
a) source ~/.bash_profile
b) su – username
c) Relogin to the machine

Reply

2 ne0 April 19, 2011 at 12:30 pm

In Ubuntu, the bash_profile called bashrc:
$ vim ~/.bashrc

Reply

3 Pawankumar Jajara August 16, 2011 at 7:34 pm

Nice Website !!

I always find decent solutions for Linux commands/script problems and they are handy and ready to copy paste. I would recommend anyone using the content on this website to directly execute on their machines.

Thanks,
~Pawan

Reply

4 Noor Kaloon November 26, 2011 at 1:06 am

useful blog..lots of good info

I need to set 2 different java versions in my Linux environment..as one app requires jdk1.4.2_19 & another app. needs jdk1.5.0_06 (both must run at the same time on the same Linux machine Amazon AMI machine)

Any pointers on how to do this?

Thanks in advance

Reply

5 Anonymous February 4, 2014 at 8:20 pm

I assume you have both vms installed. I would create a launcher script for each program, using an absolute file path to the appropriate java executable.

Alternatively, I suppose you could leave java out of your path statement and create a launch script that appends the correct java’s bin directory to the path with the export command discussed above before launching.

Note that I have not tested either option but I see no reason why they wouldn’t work.

Reply

6 SaliproIT March 27, 2012 at 3:21 pm

thanks so much! It help me solving many problem.

Reply

7 Juan August 2, 2012 at 6:04 pm

Thanks for the post.

There’s a typo though, where it says:
vi ~/.bash_proflle

it should be:
vi ~/.bash_profile

Reply

8 lion March 20, 2013 at 3:47 am

GREAT,

Reply

9 raghavendra K September 19, 2012 at 7:10 pm

thanks ! it helped me to know some things about linux.

Reply

10 Pradeep Kumar September 20, 2012 at 1:47 pm

Hello Vivek,

For a beginner on linux like me, it is very helpful. Thanks for the post.

-Pradeep

Reply

11 lalio October 2, 2012 at 12:12 am

Hello, I was confused a bit for the command export: in some site and document, sometime the commands for setting the path are:
export PATH=$PATH:/blabla
export PATH=”$PATH”:/blabla
and export PATH=${PATH}:/blabla (as you wrote above)
So I wonder which one is the correct command and if they are right in some situation, which one should I use for which case? Please specify them for me. I’m learning more about Linux. Thank you in advance

Reply

12 Hitesh Prajapti December 20, 2012 at 12:50 pm

Thanks, Providing good information.

Reply

13 raju sahani March 13, 2013 at 8:07 am

thanks 2 providing good knowledge

Reply

14 lion March 20, 2013 at 3:42 am

hi ./Amit.sh
BY THE WAY;

maybe “init q” is useful; ah…must redhat os;

Reply

15 laike9m September 8, 2013 at 1:35 am

This really helps!

Reply

16 Steven February 6, 2014 at 11:57 pm

convert was exiting with the message “Killed” or just outright segfaulting on me.

I set CONVERT_ARGS to “-limit memory 1 -limit map 1″ as suggested here: http://www.imagemagick.org/discourse-server/viewtopic.php?f=3&t=12623

That solved the problem. It probably slowed things down a bit, but that wasn’t a big deal for me.

Reply

17 Steven February 7, 2014 at 12:00 am

I posted this to the wrong tab.. oops.

Reply

18 Santak Dalai May 10, 2014 at 2:25 pm

Hello,

I am facing a situation where I have to link a library at run time. I don’t have LD_RUN_PATH defined in my system. When I try to set this variable using: setenv $LD_RUN_PATH=usr/lib . But I get this error message: LD_RUN_PATH: Undefined variable.
Is there a way to create an environment variable in linux?

Reply

19 Mahendra Thapa June 27, 2014 at 2:23 am

Very good documentation !!! I am able to fix my problem(s) just following steps.

Reply

Leave a Comment

Tagged as: , , , , , , , , , , , , , , , , , , , ,

Previous Faq:

Next Faq: