<分区>
这是我在为 Linux 创建命令行应用程序工具时偶然发现的一个非常有趣的问题。不幸的是,关于 SO 的答案隐藏在对其他问题的无数答案中,以至于我决定为那些想要以编程方式修改 PATH 的人再问一个关于 SO 的问题。
<分区>
这是我在为 Linux 创建命令行应用程序工具时偶然发现的一个非常有趣的问题。不幸的是,关于 SO 的答案隐藏在对其他问题的无数答案中,以至于我决定为那些想要以编程方式修改 PATH 的人再问一个关于 SO 的问题。
最佳答案
Grzegorz Żur's answer另一个问题很好地捕捉到了它。不幸的是,它被隐藏在许多其他答案中。
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.
此外,/etc/environment
不是脚本文件,而是由赋值表达式组成,每行一个。由于此文件存储系统范围的区域设置和路径设置,因此它是最常引用的选择。
使用 /etc/profile
不是首选。它的存在只是为了指向/etc/bash.bashrc
。并从 /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.
关于linux - Ubuntu 中的 PATH 变量设置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37676849/