Linux 中的 $PATH 是什么以及如何修改它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16560332/
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
What is this $PATH in Linux and how to modify it
提问by ruggedbuteducated
I have a few questions on this $PATH in Linux.
我对 Linux 中的 $PATH 有几个问题。
I know it tells the shell which directories to search for executable files, so:
我知道它告诉 shell 搜索可执行文件的目录,所以:
- What does it mean an environmental variable?
- How to change its path? and is it recommended to change it?
- IF i change it what are the consequences?
- 环境变量是什么意思?
- 如何改变它的路径?是否建议更改它?
- 如果我改变它会有什么后果?
采纳答案by Chris
To get your path current $PATH
variable type in:
要获取您的路径当前$PATH
变量类型:
echo $PATH
It tells your shell where to look for binaries.
它告诉你的 shell 在哪里寻找二进制文件。
Yes, you can change it - for example add to the $PATH
folder with your custom scripts.
是的,您可以更改它 - 例如添加到$PATH
带有自定义脚本的文件夹中。
So: if your scripts are in /usr/local/myscripts
to execute them you will have to type in a full path to the script: /usr/local/myscripts/myscript.sh
After changing your $PATH
variable you can just type in myscript.sh
to execute script.
所以:如果您的脚本/usr/local/myscripts
要执行它们,您将必须输入脚本的完整路径:/usr/local/myscripts/myscript.sh
更改$PATH
变量后,您只需输入myscript.sh
即可执行脚本。
Here is an example of $PATH
from RHEL:
这是$PATH
来自 RHEL的示例:
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/user/bin
/usr/kerberos/bin:/usr/local/bin:/bin:/usr/bin:/home/user/bin
To change your $PATH
you have to either edit ~/.profile
(or ~/.bash_profile
) for user or global $PATH
setting in /etc/profile
.
要更改您的用户或全局设置,$PATH
您必须编辑~/.profile
(或~/.bash_profile
).$PATH
/etc/profile
One of the consequences of having inaccurate $PATH
variables is that shell will not be able to find and execute programs without a full $PATH
.
$PATH
变量不准确的后果之一是,如果没有完整的$PATH
.
回答by rspencer
Firstly, you are correct in your statement of what $PATH does. If you were to break it somehow (as per your third point), you will have to manually type in /usr/bin/xyz if you want to run a program in /usr/bin from the terminal. Depending on how individual programs work, this might break some programs that invoke other ones, as they will expect to just be able to run ls or something.
首先,您对 $PATH 功能的陈述是正确的。如果您要以某种方式破坏它(根据您的第三点),如果您想从终端在 /usr/bin 中运行程序,则必须手动输入 /usr/bin/xyz 。根据单个程序的工作方式,这可能会破坏一些调用其他程序的程序,因为他们期望只能运行 ls 或其他东西。
So if you were to play around with $PATH, I would suggest saving it somewhere first. Use the command line instruction
因此,如果您要使用 $PATH,我建议您先将其保存在某个地方。使用命令行指令
echo $PATH > someRandomFile.txt
to save it in someRandomFile.txt
将其保存在 someRandomFile.txt
You can change $PATH using the export command. So
您可以使用 export 命令更改 $PATH。所以
export PATH=someNewPath
HOWEVER, this will completely replace $PATH with someNewPath. Since items in path are separated by a ":", you can add items to it (best not to remove, see above) by executing
但是,这将完全用 someNewPath 替换 $PATH。由于路径中的项目由“:”分隔,您可以通过执行向其中添加项目(最好不要删除,见上文)
export PATH=$PATH:newPath
The fact that it is an environmental variable means that programs can find out its value, ie it is something that is set about the environment that the program is running in. Other environmental variables include things like the current directory and the address of the current proxy.
它是环境变量的事实意味着程序可以找到它的值,即它是关于程序运行环境的设置。其他环境变量包括诸如当前目录和当前代理的地址之类的东西.
回答by Kolitha Warnakulasooriya
this is simple and i do like this way.
这很简单,我喜欢这种方式。
Open the linux bash shell and print the environment variables:
打开linux bash shell并打印环境变量:
printenv
I copy "PATH
" variable to a text editor and edit as I want. Then update the PATH
like this
我将“ PATH
”变量复制到文本编辑器并根据需要进行编辑。然后PATH
像这样更新
export PATH= /variable dir list/
It Works.
有用。
or if you want to add an single variable use this command.
或者如果您想添加单个变量,请使用此命令。
export PATH = $PATH:/variable_dir_path/
This will extends the PATH with your new directory path.
这将使用您的新目录路径扩展 PATH。