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
PATH environment variable in linux
提问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 PATH
can 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/profile
is meant for system setting for for Bourne & Bourne compatible shells. The behavior of /etc/profile
mayvary across distributions.
For latest Ubuntu distributions it is recommended to use /etc/environment
for system-wide settings (and it is not recommendedto use /etc/profile
or /etc/bash.bashrc
as noted in Ubuntu help link)
On Ubuntu machine, /etc/profile
is a shell scrip which sources the scripts in /etc/profile.d
& system-wide bashrc
file in /etc/bash.bashrc
whereas /etc/environment
is 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.bashrc
如Ubuntu 帮助链接中所述)
在 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/profile
and work up from there. Alternatively, strace
might show you what files the shell tries to read when it starts.
你应该开始阅读/etc/profile
并从那里开始工作。或者,strace
可能会向您显示 shell 在启动时尝试读取的文件。
For instance, here is how my /etc/profile
starts:
例如,这是我的/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