UNIX / Linux: Set your PATH Variable Using set or export Command

by on February 13, 2008 · 15 comments· LAST UPDATED October 5, 2013

in , ,

How do I add a new path to $PATH variable under Linux and UNIX like operating system? What is my path, and how do I set or modify it using csh/tcsh or bash/ksh/sh shell?

The PATH is an environment variable. It is a colon delimited list of directories that your shell searches through when you enter a command. All executables are kept in different directories on the Linux and Unix like operating systems.
Tutorial details
DifficultyEasy (rss)
Root privilegesNo
RequirementsNone
Estimated completion time5m

Finding out your current path

To find out what your current path setting, type the following command at shell prompt. Open the Terminal and then enter:

 
echo "$PATH"
 

OR

 
printf "%s\n" "$PATH"
 

Sample outputs:

/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/opt/X11/bin:/usr/local/sbin/modemZapp:/Users/vivek/gcutil-1.8.4

How do I modify my path?

To modify your path edit $PATH variable as per your shell. The syntax for setting path under UNIX / Linux dependent upon your login shell.

Bash, Sh, Ksh shell syntax to modify $PATH

If you are using bash, sh, or ksh, at the shell prompt, type:

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
export PATH=$PATH:/path/to/dir1
export PATH=$PATH:/path/to/dir1:/path/to/dir2
 

OR

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
PATH=$PATH:/path/to/dir1; export PATH
 

Please feel free to replace /path/to/dir1 with the directory you want the shell to search.

Tcsh or csh shell syntax to modify $PATH

If you are using tcsh or csh, shell enter:

 ## please note 'path' is case sensitivity and must be in lowercase ##
set path = ($path /path/to/dir1)
set path = ($path /path/to/dir1 /path/to/dir2)
 

OR

## please note 'PATH' is CASE sensitivity and must be in UPPERCASE ##
setenv PATH $PATH:/path/to/dir1
setenv PATH $PATH:/path/to/dir1:/path/to/dir2
 

Please feel free to replace /path/to/dir1 with the directory you want the shell to search.

Examples

In this example add /usr/local/bin to your path under BASH/ksh/sh shell, enter:

 
export PATH=$PATH:/usr/local/bin
 

OR

 
PATH=$PATH:/usr/local/bin; export PATH
 

To make these changes permanent, add the commands described above to the end of your ~/.profile file for sh and ksh shell, or ~/.bash_profile file for bash shell:

## BASH SHELL ##
echo 'export PATH=$PATH:/usr/local/bin'  >> ~/.bash_profile
 

KSH/sh shell user try:

## KSH / SH SHELL ##
echo 'export PATH=$PATH:/usr/local/bin'  >> ~/.profile
 

In this final example add /usr/local/bin/ and /scripts/admin/ to your path under csh / tcsh shell, enter:

 
set path = ($path /usr/local/bin /scripts/admin)
 

OR

 
setenv PATH $PATH:/usr/local/bin:/scripts/admin
 

To make these changes permanent, add the commands described above to the end of your ~/.cshrc file:

 
echo 'set path = ($path /usr/local/bin /scripts/admin)'  >> ~/.cshrc
 

OR

 
echo 'setenv PATH $PATH:/usr/local/bin:/scripts/admin'  >> ~/.cshrc
 

To verify new path settings, enter:
$ echo $PATH

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

{ 15 comments… read them below or add one }

1 Giovanni L. Uson January 27, 2009 at 4:58 am

Hello,

I am a newbie on Linux.

I would like to ask if I can include an environment variable (e.g. ARCHIVES) that points to a directory (e.g. EXPORT ARCHIVES=/some/path/directory) to the .bash_profile, so that I dont do exporting all the time, everytime I need to use the directory?

regards,
Giovanni

Reply

2 bharath September 30, 2009 at 9:59 am

How to set the CLASSPATH??

Reply

3 anonymous November 3, 2009 at 5:21 pm

Or add as follows to your .bashrc file:
“echo ‘export PATH=$PATH:/usr/local/bin’ >> ~/.bashrc”

Isn’t echo ” ‘PATH=$PATH:/usr/local/bin’ >> ~/.bashrc ” a better idea?

Reply

4 Nir December 24, 2010 at 11:12 am

Guys,

how can I remove a path variable??

Reply

5 OnlyOne August 31, 2011 at 10:20 am

Hi,
To remove a path, go to”File System”. Open
/etc folder and edit (that is, remove )the path from the ‘environment’ text file. You can edit using the sudo command. Following are the commands.
cd ~
cd etc
sudo gedit environment

After removing the path from the “environment” file, save and restart the machine

Reply

6 OnlyOne August 31, 2011 at 10:29 am

Hi,
There was one mistake. It is “cd /”, not “cd ~”
To remove a path, go to”File System”. Open
/etc folder and edit (that is, remove )the path from the ‘environment’ text file. You can edit using the sudo command. Following are the commands.
cd /
cd etc
sudo gedit environment

After removing the path from the “environment” file, save and restart the machine

Reply

7 Sreejith April 27, 2011 at 7:02 am

To add a PATH for any user with sh or bash shell permanantly use the following steps.

1. Create a new file .profile in root(/) directory.
2. Add the following lines into it
PATH= path to enter
export PATH
3.save the file
4.exit and login to server again
5.check using echo $PATH

IT will work. Please let me know if tou have any queries on this !!!

Sreejith

Reply

8 Sreejith April 27, 2011 at 7:04 am

The above one is only for root user

Reply

9 alberto June 6, 2011 at 12:21 am

When I run my program I get this result:
terminate called after throwing an instance of ‘std::logic_error’
what(): basic_string::_S_construct NULL not valid
Aborted

Is this a result of having the wrong environment variable on my path or what. The program compiles without any errors. This is happening on Ubuntu (Linux, OS 10.0)

Reply

10 Sumanth June 11, 2012 at 9:33 am

Hi ,

Could any one explain me about the functionality of command in shell script
set -xv
. /opt/app/etl/bin/profile.ksh
. `dirname $0`/env.cfg

Reply

11 souji June 13, 2013 at 8:56 am

Hi Sumanth,

Print input commands and their arguments as they are executed –> when you use set -xv

Reply

12 Eric October 5, 2013 at 1:32 am

Hi there, thanks fo the article!

FYI, I just tried the syntax above for a tcsh but it didn’t work.
This works:

setenv PATH ${PATH}:${HOME}/bin:.

(Include this line directly in your .cshrc file. This example adds a dir called ~/bin and your current dir to the previously existing PATH)

(Or, if you don’t want to open and edit your ~/.cshrc file, type this in a teminal:)

echo 'setenv PATH ${PATH}:${HOME}/bin:.' >> ~/.cshrc

Reply

13 nixCraft October 5, 2013 at 8:05 am

Hi,

Thanks for the heads up. The faq has been updated with correct syntax. FYI, the syntax setenv PATH ${PATH}:${HOME}/bin:. can be updated using the following syntax too:

 ## please note 'path' is case sensitivity and must be in lowercase ##
set path = ($path $HOME/bin .)
echo $path
echo $PATH

Appreciate your post.

Reply

14 satish November 28, 2013 at 5:22 pm

it helps me lots thanks………….

Reply

15 Mcgrady Chen February 6, 2014 at 8:40 am

Hi :
I am new to linux.
May I ask how to convert this bash to tcsh?

#remove faulty version of Makefile from $PATH
export PATH$(echo $PATH | tr `:` `\n` | awk `$0 != "/usr/local/bin"` | paste -sd:)

Best Regards,
McGrady

Reply

Leave a Comment

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

Previous Faq:

Next Faq: