Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is PATH variable set in Ubuntu? [duplicate]

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.

like image 254
John Strood Avatar asked Jun 07 '16 10:06

John Strood


People also ask

Where is the PATH variable stored in Ubuntu?

Ubuntu uses /etc/environment for the system-wide configuration and you find the default paths in this file.

Where is $PATH variable stored?

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.

Where does PATH variable Get Set Linux?

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.


2 Answers

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

  1. /etc/environment List of unique assignments. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME.
  2. /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.
  3. /etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells.
  4. /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

  1. ~/.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 including HOME or PATH so it has limited use.
  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 script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems.
  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific.
like image 83
John Strood Avatar answered Sep 19 '22 11:09

John Strood


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
like image 23
Mateusz Bartocha Avatar answered Sep 22 '22 11:09

Mateusz Bartocha