This is a very interesting question that I stumbled upon when I was creating a command-line application tool for Linux. Unfortunately, the answer on SO is so hidden among the myriad answers to other questions that I decided to ask another question on SO for those who want to modify PATH programmatically.
Ubuntu uses /etc/environment for the system-wide configuration and you find the default paths in this file.
The PATH environment variable is an important security control. It specifies the directories to be searched to find a command. The default systemwide PATH value is specified in the /etc/profile file, and each user normally has a PATH value in the user's $HOME/. profile file.
The PATH variable can be set in the ~/. profile file. As in all prior examples, we will need to source these changes to make them active for the current shell, but subsequent logins will persist the changes.
Grzegorz Żur's answer to another question captures it brilliantly. Unfortunately it was hidden away among many other answers.
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.
System wide
/etc/environment
List of unique assignments. Perfect for adding system-wide directories like/usr/local/something/bin
toPATH
variable or definingJAVA_HOME
./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 forPATH
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./etc/profile
and/etc/profile.d/*
Shell script. This is a good choice for shell-only systems. Those files are read only by shells./etc/<shell>.<shell>rc
. Shell script. This is a poor choice because it is single shell specific.
Also, /etc/environment
is not a script file, but rather consists of assignment expressions, one per line. Since this file stores the system-wide locale and path settings, it is most oft quoted choice.
Using /etc/profile
is not preferred. It exists only to point to /etc/bash.bashrc
and to collect entries from /etc/profile.d
User session
~/.pam_environment
. List of unique assignments. 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 variable includingHOME
orPATH
so it has limited use.~/.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 extendingPATH
with values such as~/bin
or~/go/bin
or defining user specificGOPATH
orNPM_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.~/.profile
Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems.~/.<shell>rc
. Shell script. This is a poor choice because it is single shell specific.
For temporary change:
~$ export PATH=$PATH:~/root/scripts
For permanent change, you can add this line to the /etc/environment
file like this:
PATH=$PATH:~/root/scripts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With