Set Path
Set Path
Sample outputs:
/home/vivek/perl5/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games
OR
PATH=/new/dir1/here:$PATH
PATH=/new/dir1/here:/new/dir2/here/:$PATH
OR
## ~/bin/ to be searched before all other directories ##
PATH=$HOME/bin/:$PATH
The first syntax adds $HOME/bin/ at the end PATH. The second syntax adds $HOME/bin/ at the
beginning of PATH.
Permanently add a directory to shell PATH
To permanently add shell path edit ~/.profile ~/.bash_profile file:
The PATH is an important concept when working on the command line. It's a list of directories that
tell your operating system where to look for programs, so that you can just write script instead of
/home/me/bin/script or C:\Users\Me\bin\script. But different operating systems
have different ways to add a new directory to it:
Windows
1. The first step depends which version of Windows you're using:
• If you're using Windows 8 or 10, press the Windows key, then search for and select "System
(Control Panel)".
• If you're using Windows 7, right click the "Computer" icon on the desktop and click
"Properties".
2. Click "Advanced system settings".
3. Click "Environment Variables".
4. Under "System Variables", find the PATH variable, select it, and click "Edit". If there is no
PATH variable, click "New".
5. Add your directory to the beginning of the variable value followed by ; (a semicolon). For
example, if the value was C:\Windows\System32, change it to C:\Users\Me\
bin;C:\Windows\System32.
6. Click "OK".
7. Restart your terminal.
Mac OS X
1. Open the .bash_profile file in your home directory (for example, /Users/your-
user-name/.bash_profile) in a text editor.
2. Add export PATH="your-dir:$PATH" to the last line of the file, where your-dir is
the directory you want to add.
3. Save the .bash_profile file.
4. Restart your terminal.
Linux
1. Open the .bashrc file in your home directory (for example, /home/your-user-
name/.bashrc) in a text editor.
2. Add export PATH="your-dir:$PATH" to the last line of the file, where your-dir is
the directory you want to add.
3. Save the .bashrc file.
4. Restart your terminal.
There are multiple ways to do it. The actual solution depends on the purpose.
The variable values are usually stored in either a list of assignments or a shell script that is run at the
start of the system or user session. In case of the shell script you must use a specific shell syntax
and export or set commands.
System wide
1. /etc/environment List of unique assignments, allows references. Perfect for adding
system-wide directories like /usr/local/something/bin to PATH variable or
defining JAVA_HOME. Used by PAM and SystemD.
2. /etc/environment.d/*.conf List of unique assignments, allows references. Perfect
for adding system-wide directories like /usr/local/something/bin to PATH
variable or defining JAVA_HOME. The configuration can be split into multiple files, usually
one per each tool (Java, Go, NodeJS). Used by SystemD that by design do not pass those
values to user login shells.
3. /etc/xprofile Shell script executed while starting X Window System session. This is
run for every user that logs into X Window System. It is a good choice for PATH entries that
are valid for every user like /usr/local/something/bin. The file is included by
other script so use POSIX shell syntax not the syntax of your user shell.
4. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-
only systems. Those files are read only by shells in login mode.
5. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell
specific. Used in non-login mode.
User session
1. ~/.pam_environment. List of unique assignments, no references allowed. Loaded by
PAM at the start of every user session irrelevant if it is an X Window System session or
shell. You cannot reference other variables including HOME or PATH so it has limited use.
Used by PAM.
2. ~/.xprofile Shell script. This is executed when the user logs into X Window System
system. The variables defined here are visible to every X application. Perfect choice for
extending PATH with values such as ~/bin or ~/go/bin or defining user specific
GOPATH or NPM_HOME. The file is included by other script so use POSIX shell syntax not
the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see
those values.
3. ~/.profile, ~/.<shell>_profile, ~/.<shell>_login Shell script. It will be
visible only for programs started from terminal or terminal emulator. It is a good choice for
shell-only systems. Used by shells in login mode.
4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used
by shells in non-login mode.
Notes
Gnome on Wayland starts user login shell to get the environment. It effectively uses login shell
configurations ~/.profile, ~/.<shell>_profile, ~/.<shell>_login files.
Manuals
• environment
• environment.d
• bash
• dash