bash 弄乱了 Ubuntu 16.04 中的 PATH 环境变量

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

Messed up PATH environment variable in Ubuntu 16.04

bashshellubuntuterminalpath

提问by Siddharth Prajosh

I tried installing Anaconda to get many python packages at once but had some issues with python IDLE where it said No package foundso had to manually set the path in ~/.bashrc.

我尝试安装 Anaconda 以一次获取多个 python 包,但是在 python IDLE 中遇到了一些问题,它说No package found必须手动设置~/.bashrc.

Once I set the path in ~/.bashrcthe IMPORT ERRORin python IDLE was solved but I'm unable to use commands on terminal now.

一旦我设定的路径~/.bashrcIMPORT ERROR在Python IDLE得到了解决,但我无法现在终端使用的命令。

I'm getting this error all the time.

我一直收到这个错误。

sid@sids-ubuntu:~$ ls
Command 'ls' is available in '/bin/ls'
The command could not be located because '/bin' is not included in the PATH environment variable.
ls: command not found
sid@sids-ubuntu:~$ sudo
Command 'sudo' is available in '/usr/bin/sudo'
The command could not be located because '/usr/bin' is not included in the PATH environment variable.
sudo: command not found
sid@sids-ubuntu:~$ mkdir aa
Command 'mkdir' is available in '/bin/mkdir'
The command could not be located because '/bin' is not included in the PATH environment variable.
mkdir: command not found

I did export PATH=/usr/bin:/binto find out my $PATHand content of /etc/environment. It seems both are different.

我确实是export PATH=/usr/bin:/bin为了找出我的$PATH和内容/etc/environment。好像两者都不一样。

sid@sids-ubuntu:~$ export PATH=/usr/bin:/bin
sid@sids-ubuntu:~$ echo $PATH
/usr/bin:/bin
sid@sids-ubuntu:~$ cat /etc/environment 
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"

Doesn't Ubuntu look for $PATHin /etc/environment? If yes, what could be the reason my $PATHis different from /etc/environment? and please help me fix it! If not, where does Ubuntu look for $PATH? please help me fix it!

不Ubuntu的寻找$PATH/etc/environment?如果是,可能是什么原因 my$PATH与 不同/etc/environment?请帮我修复它!如果没有,Ubuntu 在哪里寻找$PATH?请帮我修复它!

回答by Sjoerd

Be aware that your /etc/environment is only re-read at reboot.

请注意,您的 /etc/environment 只会在重新启动时重新读取。

When you want to change your path, be sure to include the existing part as well. To do that, add $PATH in the new path definition.

当你想改变你的路径时,一定要包括现有的部分。为此,请在新路径定义中添加 $PATH。

export PATH="$PATH:/usr/bin"

Looking at your problems, adding the $PATH in your ~/.bashrc should do the trick. If not, open a new terminal and show us the output of

查看您的问题,在 ~/.bashrc 中添加 $PATH 应该可以解决问题。如果没有,打开一个新终端并向我们展示输出

echo $PATH

回答by Tombart

When adding some directory to PATHit's good idea not to overwrite previous value, just append desired directory (e.g. $HOME/bin), in your ~/.bashrcadd at the end line (and remove any previous tampering with PATH)

添加某个目录时,PATH最好不要覆盖以前的值,只需将所需的目录(例如$HOME/bin~/.bashrc添加到末尾行的添加中(并删除之前的任何篡改PATH

export PATH="$PATH:$HOME/bin"

and run:

并运行:

source ~/.bashrc

(or just open new session of terminal).

(或只是打开新的终端会话)。

回答by user1934428

PATH is an environment variable, and therefore it is not looked up in any file.

PATH 是一个环境变量,因此不会在任何文件中查找。

There are several files which are sourced when bash is invoked (see the section named INVOCATIONin the bash man page), and while sourcing these files, the environment variable PATH can be set, respectively manipulated. Note that .bashrcis notalways processed; please read the bash man-page carefully to understand, which files are included under which condition.

调用 bash 时有几个文件来源(请参阅bash 手册页中名为INVOCATION的部分),在来源这些文件时,可以设置环境变量 PATH,分别操作。请注意,.bashrc不是总是处理; 请仔细阅读 bash 手册页以了解在何种条件下包含哪些文件。