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
/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 script. This is a poor choice because it is single shell specific.. rc
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
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.
~/.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 script. This is a poor choice because it is single shell specific.rc
No comments:
Post a Comment