如何在 Linux/Unix 上永久设置 $PATH [英] How to permanently set $PATH on Linux/Unix
问题描述
我正在尝试将一个目录添加到我的路径中,以便它始终在我的 Linux 路径中.我试过了:
I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
export PATH=$PATH:/path/to/dir
这是可行的,但是每次我退出终端并启动一个新的终端实例时,此路径都会丢失,我需要再次运行导出命令.
This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again.
如何才能永久设置此设置?
How can I do it so this will be set permanently?
推荐答案
有多种方法可以做到.实际的解决方案取决于目的.
There are multiple ways to do it. The actual solution depends on the purpose.
变量值通常存储在分配列表或在系统或用户会话开始时运行的 shell 脚本中.如果是 shell 脚本,您必须使用特定的 shell 语法和 export
或 set
命令.
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 and export
or set
commands.
/etc/environment
唯一分配的列表.允许参考.非常适合将/usr/local/something/bin
等系统范围的目录添加到PATH
变量或定义JAVA_HOME
.由 PAM 和 systemd.
/etc/environment
List of unique assignments. Allows references. Perfect for adding system-wide directories like/usr/local/something/bin
toPATH
variable or definingJAVA_HOME
. Used by PAM and systemd.
/etc/environment.d/*.conf
唯一分配的列表.允许参考.非常适合将 /usr/local/something/bin
等系统范围的目录添加到 PATH
变量或定义 JAVA_HOME
.配置可以拆分为多个文件,通常每个工具(Java、Go 和 Node.js)一个.由 systemd 使用,设计上不会将这些值传递给用户登录 shell.
/etc/environment.d/*.conf
List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin
to PATH
variable or defining JAVA_HOME
. The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). Used by systemd that by design do not pass those values to user login shells.
/etc/xprofile
在启动 X Window System 会话时执行的 Shell 脚本.这是为每个登录 X Window System 的用户运行的.对于像 /usr/local/something/bin
这样对每个用户都有效的 PATH
条目来说,这是一个不错的选择.该文件包含在其他脚本中,因此请使用 POSIX shell 语法而不是用户 shell 的语法.
/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.
/etc/profile
和 /etc/profile.d/*
Shell 脚本.这对于仅使用 shell 的系统来说是一个不错的选择.这些文件在登录模式下只能由 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 in login mode.
/etc/
.外壳脚本.这是一个糟糕的选择,因为它是特定于单个 shell 的.在非登录模式下使用.
/etc/<shell>.<shell>rc
. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
用户会话
~/.pam_environment
.唯一分配列表,不允许引用.由 PAM 在每个用户会话开始时加载,如果它是 X Window 系统会话或 shell,则无关紧要.您不能引用其他变量,包括HOME
或PATH
,因此它的用途有限.由 PAM 使用.
~/.pam_environment
. List of unique assignments, no references allowed. 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 variables includingHOME
orPATH
so it has limited use. Used by PAM.
~/.xprofile
Shell 脚本.这是在用户登录 X Window System 系统时执行的.此处定义的变量对每个 X 应用程序都是可见的.使用 ~/bin
或 ~/go/bin
等值扩展 PATH
或定义用户特定的 GOPATH
的完美选择> 或 NPM_HOME
.该文件包含在其他脚本中,因此请使用 POSIX shell 语法而不是用户 shell 的语法.通过快捷方式启动的图形文本编辑器或 IDE 将看到这些值.
~/.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.
~/.profile
、~/.
、~/.
外壳脚本.它仅对从终端或终端模拟器启动的程序可见.对于只有 shell 的系统来说,它是一个不错的选择.由 shell 在登录模式下使用.
~/.profile
, ~/.<shell>_profile
, ~/.<shell>_login
Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.
~/.
.外壳脚本.这是一个糟糕的选择,因为它是特定于单个 shell 的.由 shell 在非登录模式下使用.
~/.<shell>rc
. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
注释
GNOME 在 Wayland 启动用户登录 shell 以获取环境.它有效地使用了登录 shell 配置 ~/.profile
、~/.<shell>_profile
、~/.<shell>_login
文件.
Notes
GNOME on Wayland starts a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile
, ~/.<shell>_profile
, ~/.<shell>_login
files.
这篇关于如何在 Linux/Unix 上永久设置 $PATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!