在Linux上,如何将一个目录添加到$PATH,以便在不同的会话中保持持久性?
背景
我试图添加一个目录到我的路径,所以它将永远在我的Linux路径。我试过了:
export PATH=$PATH:/path/to/dir
这是有效的,但是每当我退出终端并启动一个新的终端实例时,这个路径就丢失了,我需要再次运行导出命令。
我怎么做才能永久设置它呢?
在Linux上,如何将一个目录添加到$PATH,以便在不同的会话中保持持久性?
背景
我试图添加一个目录到我的路径,所以它将永远在我的Linux路径。我试过了:
export PATH=$PATH:/path/to/dir
这是有效的,但是每当我退出终端并启动一个新的终端实例时,这个路径就丢失了,我需要再次运行导出命令。
我怎么做才能永久设置它呢?
当前回答
在Ubuntu中,编辑/etc/environment。它的唯一目的是存储环境变量。$PATH变量最初定义在这里。
这是我的/etc/environment文件的粘贴:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
所以你可以以根用户身份打开这个文件,添加任何你想要的东西。
为了立竿见影,
运行(以普通用户和root用户尝试):
source /etc/environment && export PATH
如果你使用Z shell (zsh),在/etc/zsh/zshenv文件的注释后面添加这一行:
source /etc/environment
我在Ubuntu 15.10 (Wily Werewolf)上遇到了这个小怪癖,但如果你的zsh没有得到正确的PATH,这可能是原因。
其他回答
添加导出命令的文件取决于您是处于登录模式还是非登录模式。
如果您处于登录模式,您要查找的文件是/etc/bash或/etc/bash.bashrc。
如果处于非登录模式,则要查找文件/。Profile或/.profiles.d目录下的文件
上面提到的文件就是系统变量所在的地方。
添加永久路径的一种方法是:
cd /etc/profile.d
touch custom.sh
vi custom.sh
export PATH=$PATH:/path according to your setting/
重启你的电脑,开始吧;路径将永远存在。
有很多种方法。实际的解决方案取决于目的。
变量值通常存储在赋值列表中或在系统或用户会话开始时运行的shell脚本中。对于shell脚本,您必须使用特定的shell语法和导出或设置命令。
系统范围
/etc/environment List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_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 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 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 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.
用户会话
~/.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 including HOME or PATH 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 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>_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.
笔记
Wayland上的GNOME启动一个用户登录shell来获取环境。它有效地使用登录shell配置~/。简介~ /。<壳> _profile ~ /。<壳> _login文件。
手册页
环境 环境。d https://linux.die.net/man/1/environment.d bash 破折号
特定的文档
Ubuntu Arch Linux
登录Shell和非登录Shell的区别?
你也可以永久地设置它,编辑这些文件之一:
/etc/profile(针对所有用户)
~ /。Bash_profile(用于当前用户)
~ /。Bash_login(用于当前用户)
~ /。配置文件(适用于当前用户)
也可以使用/etc/environment设置永久的PATH环境变量,但不支持变量展开。
提取自:Linux: Añadir ruta al PATH
我的回答是关于在Ubuntu Linux (amd64)上设置Go环境。我曾经遇到过设置环境变量(GOPATH和GOBIN)路径的相同麻烦,每次在终端退出时丢失它,并使用source <file_name>重新构建它。
错误在于将路径(GOPATH和GOBIN)放在~/中。bash_profile文件。在浪费了几个小时之后,我发现解决方案是将GOPATH和GOBIN放在~/中。Bash_rc文件的格式如下:
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH:$GOBIN
在这样做的过程中,Go安装工作得很好,没有路径丢失。
与此问题相关的原因是,对于非登录shell的设置,比如我们运行Go代码的Ubuntu终端或GNOME终端,是从~。/bash_rc文件和登录shell的设置是从~/中获取的。bash_profile文件。从~/。如果~/. Profile文件。Bash_profile文件不可达。