linux - Ubuntu 中的 PATH 变量设置在哪里?

标签 linux bash unix ubuntu path

<分区>

这是我在为 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

  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.

此外,/etc/environment不是脚本文件,而是由赋值表达式组成,每行一个。由于此文件存储系统范围的区域设置和路径设置,因此它是最常引用的选择。 使用 /etc/profile不是首选。它的存在只是为了指向/etc/bash.bashrc。并从 /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.

关于linux - Ubuntu 中的 PATH 变量设置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37676849/

相关文章:

c++ - xterm:无法 execvp:没有文件或目录

linux - 在 ARM 处理器上运行嵌入式 Linux 的最低配置?

linux - 如何使用 grep 只获取没有路径的文件名

linux - 如何将行添加到 sed 输出的开头

c++ - 从 c/c++ 代码更改 Linux 用户运行时

unix - Cmake 无法工作 - 没有当前工作目录

php - 在 Linux 上使用 PHP 将元数据或文本注入(inject) WMV

c - 使用 native 编译器和交叉编译器生成相同的目标文件

c - 我应该如何查看GDB中特定地址的堆栈数据?

git - 无法在 git bash 中生成新的 CSR