如何在 Linux/Unix 上永久设置 $PATH?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14637979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to permanently set $PATH on Linux/Unix?
提问by Click Upvote
I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
我正在尝试向我的路径添加一个目录,以便它始终在我的 Linux 路径中。我试过了:
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?
我该怎么做才能永久设置?
采纳答案by Grzegorz ?ur
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 and export
or set
commands.
变量值通常存储在分配列表或在系统或用户会话开始时运行的 shell 脚本中。对于 shell 脚本,您必须使用特定的 shell 语法和/export
或set
命令。
System wide
系统范围
/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
List of unique assignments, allows references. Perfect for adding system-wide directories like/usr/local/something/bin
toPATH
variable or definingJAVA_HOME
. The configuration can be split into multiple files, usually one per each tool (Java, Go, NodeJS). Used by SystemD that by design do not pass those values to user login shells./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 in login mode./etc/<shell>.<shell>rc
. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
/etc/environment
唯一分配列表,允许引用。完善,用于将全系统目录像/usr/local/something/bin
到PATH
变量或定义JAVA_HOME
。由 PAM 和 SystemD 使用。/etc/environment.d/*.conf
唯一分配列表,允许引用。完善,用于将全系统目录像/usr/local/something/bin
到PATH
变量或定义JAVA_HOME
。配置可以拆分为多个文件,通常每个工具(Java、Go、NodeJS)一个。由 SystemD 使用,设计上不会将这些值传递给用户登录 shell。/etc/xprofile
启动 X Window System 会话时执行的 Shell 脚本。这是为每个登录 X Window System 的用户运行的。对于PATH
对每个用户都有效的条目,例如/usr/local/something/bin
. 该文件包含在其他脚本中,因此请使用 POSIX shell 语法而不是用户 shell 的语法。/etc/profile
和/etc/profile.d/*
Shell 脚本。这对于仅使用 shell 的系统来说是一个不错的选择。这些文件只能在登录模式下由 shell 读取。/etc/<shell>.<shell>rc
. 外壳脚本。这是一个糟糕的选择,因为它是特定于单个 shell 的。在非登录模式下使用。
User session
用户会话
~/.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 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>_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>rc
. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
~/.pam_environment
. 唯一分配列表,不允许引用。由 PAM 在每个用户会话开始时加载,如果它是 X Window 系统会话或 shell,则无关紧要。你不能引用其他变量,包括HOME
或PATH
因此具有有限的用途。由 PAM 使用。~/.xprofile
外壳脚本。这是在用户登录 X Window System 系统时执行的。此处定义的变量对每个 X 应用程序都是可见的。PATH
使用值进行扩展的完美选择,例如~/bin
or~/go/bin
或定义用户特定的GOPATH
orNPM_HOME
。该文件包含在其他脚本中,因此请使用 POSIX shell 语法而不是用户 shell 的语法。通过快捷方式启动的图形文本编辑器或 IDE 将看到这些值。~/.profile
,~/.<shell>_profile
,~/.<shell>_login
Shell 脚本。它仅对从终端或终端模拟器启动的程序可见。对于只有 shell 的系统来说,它是一个不错的选择。由 shell 在登录模式下使用。~/.<shell>rc
. 外壳脚本。这是一个糟糕的选择,因为它是特定于单个 shell 的。由 shell 在非登录模式下使用。
Notes
笔记
Gnome on Wayland starts user login shell to get the environment. It effectively uses login shell configurations ~/.profile
, ~/.<shell>_profile
, ~/.<shell>_login
files.
Wayland 上的 Gnome 启动用户登录 shell 以获取环境。它有效地使用登录外壳配置~/.profile
、~/.<shell>_profile
、~/.<shell>_login
文件。
Manuals
手册
- environment
- environment.d
- bash
- dash
- 环境
- 环境文件
- 猛击
- 短跑
Distribution specific documentation
分发特定文件
Related
有关的
回答by aqua
You can add that line to your console config file (e.g. .bashrc) , or to .profile
您可以将该行添加到您的控制台配置文件(例如 .bashrc)或 .profile
回答by Fraxtil
Put the export
declaration in ~/.bashrc
. My .bashrc contains this:
将export
声明放入~/.bashrc
. 我的 .bashrc 包含这个:
export PATH=/var/lib/gems/1.8/bin:/home/fraxtil/.bin:$PATH
回答by mpowered
You need to add it to your ~/.profile
or ~/.bashrc
file.
您需要将其添加到您的~/.profile
或~/.bashrc
文件中。
export PATH="$PATH:/path/to/dir"
Depending on what you're doing, you also may want to symlink to binaries:
根据您在做什么,您可能还想符号链接到二进制文件:
cd /usr/bin
sudo ln -s /path/to/binary binary-name
Note that this will not automatically update your path for the remainder of the session.To do this, you should run:
请注意,这不会在会话的剩余时间自动更新您的路径。为此,您应该运行:
source ~/.profile
or
source ~/.bashrc
回答by Edward Torvalds
the best simple way is the following line:PATH="<directory you want to include>:$PATH"
in your .bashrc file in home directory.
It will not get reset even if you close the terminal or reboot your PC. Its permanent
最好的简单方法是以下行:PATH="<directory you want to include>:$PATH"
在主目录中的 .bashrc 文件中。
即使您关闭终端或重新启动 PC,它也不会被重置。它的永久
回答by Dikinha
the files where you add the export command depends if you are in login-mode or non-login-mode.
添加导出命令的文件取决于您是处于登录模式还是非登录模式。
if you are in login-mode, the files you are looking for is either /etc/bash or /etc/bash.bashrc
如果您处于登录模式,则您要查找的文件是 /etc/bash 或 /etc/bash.bashrc
if you are in non-login-mode, you are looking for the file /.profile or for the files within the directory /.profiles.d
如果您处于非登录模式,您正在寻找文件 /.profile 或目录 /.profiles.d 中的文件
the files mentioned above if where the system variables are.
上面提到的文件,如果系统变量在哪里。
回答by Mohit M
You may set $PATH
permanently in 2 ways.
您可以$PATH
通过 2 种方式永久设置。
To set path for particular user : You may need to make the entry in
.bash_profile
in home directory in the user.e.g in my case I will set java path in tomcat user profile
[tomcat]$ echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
To set common path for ALL system users, you may need to set path like this :
[root~]# echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
为特定用户设置路径:您可能需要
.bash_profile
在用户的主目录中进行输入。例如,在我的情况下,我将在 tomcat 用户配置文件中设置 java 路径
[tomcat]$ echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
要为所有系统用户设置通用路径,您可能需要像这样设置路径:
[root~]# echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
回答by trve.fa7ad
In Ubuntu, edit /etc/environment
. Its sole purpose is to store Environment Variables. Originally the $PATH variable is defined here.
This is a paste from my /etc/environment
file:
在 Ubuntu 中,编辑/etc/environment
. 它的唯一目的是存储环境变量。最初在这里定义了 $PATH 变量。这是我的/etc/environment
文件中的粘贴:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
So you can just open up this file as root and add whatever you want.
所以你可以以 root 身份打开这个文件并添加任何你想要的。
For Immediate results, Run (try as normal user androot):
对于立即结果,运行(以普通用户和root身份尝试):
source /etc/environment && export PATH
UPDATE:
更新:
If you use zsh
(a.k.a Z Shell), add this line right after the comments in /etc/zsh/zshenv
:
如果您使用zsh
(又名 Z Shell),请在以下注释后立即添加此行/etc/zsh/zshenv
:
source /etc/environment
I encountered this little quirk on Ubuntu 15.10, but if your zshis not getting the correct PATH, this could be why
我在 Ubuntu 15.10 上遇到了这个小怪癖,但是如果您的zsh没有获得正确的PATH,这可能就是原因
回答by user3439968
Permanently add PATH variable
永久添加 PATH 变量
Global:
全球的:
echo "export PATH=$PATH:/new/path/variable" >> /etc/profile
Local(for user only):
本地(仅限用户):
echo "export PATH=$PATH:/new/path/variable" >> ~/.profile
For globalrestart. For localrelogin.
用于全局重启。用于本地重新登录。
Example
例子
Before:
前:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
After:
后:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Alternatively you can just edit profile:
或者,您可以只编辑个人资料:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Another way(thanks gniourf_gniourf):
另一种方式(感谢 gniourf_gniourf):
echo 'PATH=$PATH:/new/path/variable' >> /etc/profile
You shouldn't use double quotes here! echo 'export PATH=$PATH:/new/path/variable'... And by the way, the export keyword is very likely useless as the PATH variable is very likely already marked as exported. – gniourf_gniourf
你不应该在这里使用双引号!echo 'export PATH=$PATH:/new/path/variable'... 顺便说一句,export 关键字很可能没用,因为 PATH 变量很可能已经被标记为已导出。– gniourf_gniourf
回答by Yuriy
Add to /etc/profile.d
folder script [name_of_script].sh
with line: export PATH=$PATH:/dir
. Every script within /etc/profile.d
folder is automaticaly executed by /etc/profile
on login.
添加到/etc/profile.d
文件夹脚[name_of_script].sh
本行:export PATH=$PATH:/dir
。/etc/profile.d
文件夹中的每个脚本在/etc/profile
登录时都会自动执行。