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.

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"

echo "$PATH"

OR

printf "%sn" "$PATH"

printf "%sn" "$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

## 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 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)

## 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 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

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

OR

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

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

## 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

## 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)

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

OR

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

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

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

OR

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

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

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

See also
  • Man pages – tcsh(1)

Posted by: SXI ADMIN

The author is the creator of SXI LLC and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.