Linux中的PATH环境变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7510249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:18:42  来源:igfitidea点击:

PATH environment variable in linux

linuxenvironment-variables

提问by pradeepchhetri

I want to know how the standard environment variables given by the linux OS like PATH, HOME are set automatically. Which file(s) are these read from. There should be some file from which these variables are set when a particular user logs in.

我想知道 linux 操作系统给出的标准环境变量如 PATH、HOME 是如何自动设置的。这些是从哪些文件中读取的。当特定用户登录时,应该有一些文件从中设置这些变量。

采纳答案by another.anon.coward

I would like a little more details to what @cnicutar has already mentioned.
Environment variables including PATHcan be

我想要更多关于@cnicutar 已经提到的细节。
环境变量包括PATH可以

  • System wide - The values of the environment variables last till the system is up
  • Session wide - Lasts till a session lasts (till user logs out)
  • 系统范围 - 环境变量的值持续到系统启动
  • 会话范围 - 持续到会话持续(直到用户注销)

/etc/profileis meant for system setting for for Bourne & Bourne compatible shells. The behavior of /etc/profilemayvary across distributions.
For latest Ubuntu distributions it is recommended to use /etc/environmentfor system-wide settings (and it is not recommendedto use /etc/profileor /etc/bash.bashrcas noted in Ubuntu help link)
On Ubuntu machine, /etc/profileis a shell scrip which sources the scripts in /etc/profile.d& system-wide bashrcfile in /etc/bash.bashrcwhereas /etc/environmentis a text file consisting of variable assignments per line which are set into the system-wide environment.

/etc/profile用于 Bourne & Bourne 兼容 shell 的系统设置。的行为/etc/profile可能因发行版而异。
对于最新的 Ubuntu 发行版,建议/etc/environment用于系统范围的设置(不建议使用/etc/profile/etc/bash.bashrcUbuntu 帮助链接中所述
在 Ubuntu 机器上,/etc/profile是一个 shell 脚本,它在/etc/profile.d& 系统范围bashrc文件中提供脚本,/etc/bash.bashrc/etc/environment是一个文本文件,每行由变量赋值组成,这些赋值被设置到系统范围的环境中。

For each user the value of environment variables including PATH(for the shell) can also be manipulated through ~/.profile, ~/.bash_profile, ~./bash_login& ~/.bashrc(~=> User's home directory)

对于每个用户,PATH还可以通过~/.profile, ~/.bash_profile, ~./bash_login& ~/.bashrc( ~=> 用户的主目录)操作包括(对于外壳程序)在内的环境变量的值

You can refer to the following link for more details on environment variable on Ubuntu System: https://help.ubuntu.com/community/EnvironmentVariables
Hope this helps!

您可以参考以下链接了解有关 Ubuntu 系统环境变量的更多详细信息:https: //help.ubuntu.com/community/EnvironmentVariables
希望这会有所帮助!

回答by cnicutar

There's nothing magic about them, the shell sets them when it starts up.

它们没有什么神奇之处,shell 在启动时设置它们。

You should start reading /etc/profileand work up from there. Alternatively, stracemight show you what files the shell tries to read when it starts.

你应该开始阅读/etc/profile并从那里开始工作。或者,strace可能会向您显示 shell 在启动时尝试读取的文件。

For instance, here is how my /etc/profilestarts:

例如,这是我的/etc/profile开始方式:

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

After that is sources other files which in their turn modify PATH.

之后是源其他文件,它们依次修改PATH.

回答by beahacker

You can even set and manage your environment variables by yourself by using module enviroment software http://en.wikipedia.org/wiki/Environment_Modules_(software). To install "module"you can use this command for Centos

您甚至可以使用模块环境软件http://en.wikipedia.org/wiki/Environment_Modules_(software)自行设置和管理您的环境变量。要安装“模块”,您可以在 Centos 中使用此命令

sudo yum install environment-modules

For Ubuntu users, you can install it from source: http://modules.sourceforge.net/

对于 Ubuntu 用户,您可以从源代码安装它:http: //modules.sourceforge.net/

Write a module file to set or prepend environment variables. Then you modules to load this file. For example, my files name "mypath"

编写一个模块文件来设置或添加环境变量。然后你模块加载这个文件。例如,我的文件名为“mypath”

proc ModulesHelp { } {
global version prefix

puts stderr "\t Loads the environment for my installed home folder HOME/local"
}

module-whatis   "Loads the environment for my installed home folder HOME/local"

set     HOME    /home/svu/a0081742

prepend-path    PATH            $HOME/local/bin
prepend-path    LIBRARY_PATH    $HOME/local/lib
prepend-path    LD_LIBRARY_PATH $HOME/local/lib
prepend-path    LD_INCLUDE_PATH $HOME/local/include
prepend-path    MANPATH         $HOME/local/share/man

Then, you load the file with

然后,您加载文件

module load mypath

Than you can list your loaded modules with

比你可以列出你加载的模块

module list