Wednesday 30 March 2016

linux - Where is PATH variable set in Ubuntu?





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.


Answer



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/.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. ~/.rc. Shell script. This is a poor choice because it is single shell specific.



No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...