如何在Linux/Unix上永久设置$ PATH? [英] How to permanently set $PATH on Linux/Unix?

查看:67
本文介绍了如何在Linux/Unix上永久设置$ PATH?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将目录添加到我的路径,因此该目录将始终位于我的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

这有效,但是每次我退出终端并启动新的终端实例时,此路径都会丢失,并且我需要再次运行export命令.

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语法和exportset命令.

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.

  1. /etc/environment唯一分配的列表,允许引用.非常适合将系统范围的目录(例如/usr/local/something/bin)添加到PATH变量或定义JAVA_HOME.由PAM和SystemD使用.
  2. /etc/environment.d/*.conf唯一分配的列表,允许引用.非常适合将系统范围的目录(例如/usr/local/something/bin)添加到PATH变量或定义JAVA_HOME.可以将配置分为多个文件,通常每个工具(Java,Go,NodeJS)每个文件一个.由SystemD使用,在设计上不会将这些值传递给用户登录Shell.
  3. /etc/xprofile启动X Window系统会话时执行的Shell脚本.将为每个登录X Window System的用户运行此命令.对于对/usr/local/something/bin等每个用户均有效的PATH条目,这是一个不错的选择.该文件包含在其他脚本中,因此请使用POSIX Shell语法而不是用户Shell的语法.
  4. /etc/profile/etc/profile.d/* Shell脚本.对于仅使用shell的系统,这是一个不错的选择.这些文件仅在登录模式下由Shell读取.
  5. /etc/<shell>.<shell>rc. Shell脚本.这是一个糟糕的选择,因为它是特定于单个shell的.在非登录模式下使用.
  1. /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.
  2. /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, NodeJS). Used by SystemD that by design do not pass those values to user login shells.
  3. /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.
  4. /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.
  5. /etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.

用户会话

  1. ~/.pam_environment.唯一作业列表,不允许引用.如果是X Window系统会话或Shell,则在每个用户会话开始时由PAM加载.您不能引用其他变量,包括HOMEPATH,因此它的使用受到限制.由PAM使用.
  2. ~/.xprofile Shell脚本.当用户登录到X Window System系统时,将执行此操作.此处定义的变量对每个X应用程序都是可见的.用诸如~/bin~/go/bin之类的值扩展PATH或定义用户特定的GOPATHNPM_HOME的完美选择.该文件包含在其他脚本中,因此请使用POSIX Shell语法而不是用户Shell的语法.通过快捷方式启动的图形文本编辑器或IDE将看到这些值.
  3. ~/.profile~/.<shell>_profile~/.<shell>_login Shell脚本.仅对从终端或终端仿真器启动的程序可见.对于仅使用shell的系统来说,这是一个不错的选择.由Shell以登录模式使用.
  4. ~/.<shell>rc. Shell脚本.这是一个糟糕的选择,因为它是特定于单个shell的.由Shell以非登录方式使用.
  1. ~/.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.
  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>_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.
  4. ~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.

注释

Wayland上的Gnome启动用户登录外壳以获取环境.它有效地使用登录外壳程序配置~/.profile~/.<shell>_profile~/.<shell>_login文件.

  • 环境
  • environment.d
  • 重击
  • 破折号
  • Ubuntu
  • archlinux

登录外壳程序和非登录外壳程序之间的区别?

这篇关于如何在Linux/Unix上永久设置$ PATH?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆