在Linux上,如何将一个目录添加到$PATH,以便在不同的会话中保持持久性?

背景

我试图添加一个目录到我的路径,所以它将永远在我的Linux路径。我试过了:

export PATH=$PATH:/path/to/dir

这是有效的,但是每当我退出终端并启动一个新的终端实例时,这个路径就丢失了,我需要再次运行导出命令。

我怎么做才能永久设置它呢?


当前回答

修改“/etc/profile”文件: vi / etc / profile 按“I”键进入编辑模式,将光标移至文件末尾。额外的条目: 导出路径= $路径:/道路/ / dir; 按“Esc”键退出编辑模式,并按“:wq”保存文件。 使配置有效 源/ etc / profile 解释: 配置文件适用于所有用户。如果希望它仅对活动用户有效,请更改“。bashrc”(文件。

其他回答

永久地添加到PATH变量中

全球:

echo "export PATH=$PATH:/new/path/variable" >> /etc/profile

本地(仅针对当前用户):

echo "export PATH=$PATH:/new/path/variable" >> ~/.profile

对于全局,请重新启动。对于本地,请重新登录。

例子

之前:

$ cat /etc/profile

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin

后:

$ cat /etc/profile

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable

或者你可以编辑文件“profile”:

$ cat /etc/profile

#!/bin/sh

export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable

另一种方式(感谢gniourf_gniourf):

echo 'PATH=$PATH:/new/path/variable' >> /etc/profile

你不应该在这里使用双引号!回声的出口 = $ PATH: /新/ PATH /变量”……顺便说一下,export关键字 是很可能无用的路径变量是很可能已经 标记为导出。——gniourf_gniourf

有很多种方法。实际的解决方案取决于目的。

变量值通常存储在赋值列表中或在系统或用户会话开始时运行的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的区别?

假设你运行的是macOS。您有一个信任的二进制文件,并希望在整个系统中可用,但不一定希望将二进制文件所在的目录添加到PATH中。

您可以选择复制/移动二进制文件到/usr/local/bin,它应该已经在您的PATH中。这将使二进制可执行文件与您在终端中已经访问的任何其他二进制文件一样。

Zues77的想法是正确的。操作人员没有说“我怎么才能破解这个?”OP想知道如何永久地附加到$PATH:

sudo nano /etc/profile

这是为所有东西设置的地方,也是为所有需要$PATH的东西更改它的最佳位置。

我昨天在寻找一种方法将包含我自己的脚本的文件夹添加到PATH时偶然发现了这个问题-并且惊讶地发现我自己的~/。配置文件(在Linux Mint 18.1上)已经包含这个:

# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
    PATH="$HOME/bin:$PATH"
fi

因此,我所要做的就是创建文件夹~/bin并将脚本放在那里。