≡ Menu

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

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
Tweet itFacebook itGoogle+ itPDF itFound an error/typo on this page?

{ 17 comments… add one }

  • Giovanni L. Uson January 27, 2009, 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

  • bharath September 30, 2009, 9:59 am

    How to set the CLASSPATH??

  • anonymous November 3, 2009, 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?

  • Nir December 24, 2010, 11:12 am

    Guys,

    how can I remove a path variable??

    • OnlyOne August 31, 2011, 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

      • OnlyOne August 31, 2011, 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

  • Sreejith April 27, 2011, 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

    • Sreejith April 27, 2011, 7:04 am

      The above one is only for root user

  • alberto June 6, 2011, 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)

  • Sumanth June 11, 2012, 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

    • souji June 13, 2013, 8:56 am

      Hi Sumanth,

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

  • Eric October 5, 2013, 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
    • nixCraft October 5, 2013, 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.

  • satish November 28, 2013, 5:22 pm

    it helps me lots thanks………….

  • Mcgrady Chen February 6, 2014, 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

  • Yahya August 4, 2015, 11:58 am

    Useful.. Thanks

  • Michael August 27, 2015, 12:57 am

    why would this code be on my computer in a install file with along with macports pubkey and several other files?????

    # $Id: setupenv.bash.in 99822 2012-11-18 11:20:14Z raimue@macports.org $
    function export_path() {
        local binpath="/opt/local/bin"
        local sbinpath="/opt/local/sbin"
        local IFS=":"
        local p
        for p in $PATH; do
            if [ "$p" == "$binpath" ]; then
                binpath=""
            elif [ "$p" == "$sbinpath" ]; then
                sbinpath=""
            fi
        done
        if [ -n "$binpath" ]; then
            binpath+=":"
        fi
        if [ -n "$sbinpath" ]; then
            sbinpath+=":"
        fi
        export PATH="${binpath}${sbinpath}${PATH}"
    }
    function export_manpath() {
        local mpath="/opt/local/share/man"
        local IFS=":"
        local p
        if [ -z "$MANPATH" ]; then
            return
        fi
        for p in $MANPATH; do
            if [ "$p" == "$mpath" ]; then
                mpath=""
            fi
        done
        if [ -n "$mpath" ]; then
            mpath+=":"
        fi
        export MANPATH="${mpath}${MANPATH}"
    }
    function export_display() {
        if [ -z "$DISPLAY" ]; then
            export DISPLAY=":0.0"
        fi
    }
    export_path
    export_manpath
    export_display
    # Remove defined functions to prevent them from cluttering the shell,
    # but they are needed to restrict variables to the local scope
    unset export_path
    unset export_manpath
    unset export_display

Leave a Comment