bash 设置 $PATH 目录的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12988393/
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
Issue with setting $PATH directories
提问by Sean
For some strange reason, I'm getting a "No such file or directory" error for my $PATHvariable. I have tried to edit my path using export, changing it from what it was originally to every permutation from a single directory path to the original.
出于某种奇怪的原因,我的$PATH变量出现“没有这样的文件或目录”错误。我尝试使用 编辑我的路径export,将其从原来的路径更改为从单个目录路径到原始路径的每个排列。
When there is one directory (e.g., export PATH=/bin), I get "/bin: Is a Directory". But once I add more than one directory (e.g., export PATH=/bin:/sbin), I get "No such file or directory".
当有一个目录(例如,export PATH=/bin)时,我得到“/bin:是一个目录”。但是一旦我添加了多个目录(例如,export PATH=/bin:/sbin),我就会得到“没有这样的文件或目录”。
I'm curious to see what the cause of this issue is!
我很想知道这个问题的原因是什么!
采纳答案by cdarke
RE; your comment:
关于; 你的评论:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/u?sr/local/mysql/bin: No such file or directorywill be generated if you have a line which says:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/u?sr/local/mysql/bin: No such file or directory如果你有一行写着:
$PATH
maybe on its own, or maybe you have $PATH=.... That is, the shell is trying to execute a program named:
也许是它自己,或者也许你有$PATH=...。也就是说,shell 正在尝试执行一个名为:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/u?sr/local/mysql/bin
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/local/git/bin:/u?sr/local/mysql/bin
Lose the $ on the left-hand side.
输掉左边的 $。
回答by paxdiablo
I'm not sure you areusing the exportvariant. You almost certainly have spaces in there and you shouldn't, as per the following transcript:
我不确定您是否在使用该export变体。根据以下成绩单,您几乎可以肯定那里有空格,而您不应该这样做:
pax> PATH= /bin
bash: /bin: is a directory
pax> PATH= /bin/sbin
bash: /bin/sbin: No such file or directory
The first is caused because you're setting the path temporarily to an empty string while attempting to run that directory. That's because you can do things like:
第一个是因为您在尝试运行该目录时将路径临时设置为空字符串。那是因为您可以执行以下操作:
pax> xyzzy=1
pax> echo $xyzzy
1
pax> xyzzy=2 bash -c 'echo $xyzzy'
2
pax> echo $xyzzy
1
In other words, it's a way of changing an environment variable for a single command, and having it automatically revert when the command is finished.
换句话说,它是一种更改单个命令的环境变量的方法,并在命令完成后自动恢复。
The second case is simply because there isno /bin/sbindirectory. So it detects that beforeit complains about the fact that you're trying to run a directory.
第二种情况是,仅仅因为是没有/bin/sbin目录。因此,它会在抱怨您尝试运行目录的事实之前检测到这一点。
Setting a variable in bashis a no-space thing (unless you have spaces in your directory names, in which case they should be quoted). In addition, they need to be colon-sparated. Hence you're looking for things like:
设置变量 inbash是无空格的事情(除非您的目录名称中有空格,在这种情况下应该引用它们)。此外,它们需要以冒号分隔。因此,您正在寻找以下内容:
PATH=/bin
PATH=/bin:/sbin
PATH="/bin:/sbin:/directory with spaces in it:$HOME/bin"
回答by alestanis
The exportfunction will only change the variable for the current terminal session.
该export函数只会更改当前终端会话的变量。
Write your PATHinside ~/.bash_profileif you want to change it permanently.
如果你想永久改变它,请写下你的PATH内心~/.bash_profile。
For this modification to work you have to close your current terminal and reopen it.
要使此修改生效,您必须关闭当前终端并重新打开它。

