在 Windows(Linux 子系统)上的 Ubuntu 上的 Bash 上设置 $PATH 的问题

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

Issues setting $PATH on Bash on Ubuntu on Windows (Linux Subsystem)

linuxbashubuntuterraformwindows-subsystem-for-linux

提问by user791134

I am using the "Bash on Ubuntu on Windows" (Linux Subsystem) and want to add Terraform to my $PATH. Since Terraform can't be installed via apt-get, I did the following steps:

我正在使用“Windows 上的 Ubuntu 上的 Bash”(Linux 子系统)并想将 Terraform 添加到我的 $PATH 中。由于无法通过apt-get安装Terraform,我做了以下步骤:

  1. Navigated to this directory, where I wanted to install Terraform:

    cd /usr/local

  2. In the above path, I used wget to download Terraform:

    wget https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip

  3. Terraform successfully unzips! When I open the file in VIM it is all good:

    unzip terraform_0.9.8_linux_amd64.zip

  4. I then enter this command to check to see if the Terraform binary is accessible from the command line:

    terraform -version

  1. 导航到这个目录,我想在其中安装 Terraform:

    cd /usr/本地

  2. 在上面的路径中,我使用wget下载Terraform:

    wget https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip

  3. Terraform 成功解压!当我在 VIM 中打开文件时,一切都很好:

    解压 terraform_0.9.8_linux_amd64.zip

  4. 然后我输入这个命令来检查是否可以从命令行访问 Terraform 二进制文件:

    地形版本

However the following message gets returned:

但是,返回以下消息:

terraform: command not found

This tells me that the Terraform downloaded location needs to be added to my $PATH.

这告诉我需要将 Terraform 下载位置添加到我的 $PATH。

  1. Already being logged in as the root user ("sudo su") I enter the following command to access ".profile":
  1. 已经以 root 用户(“sudo su”)登录,我输入以下命令来访问“.profile”:

vim ~/.profile

vim ~/.profile

The following is already in this file, which I leave untouched:

以下内容已在此文件中,我保持不变:

 # ~/.profile: executed by Bourne-compatible login shells.

 if [ "$BASH" ]; then
   if [ -f ~/.bashrc ]; then
     . ~/.bashrc
   fi
 fi

 mesg n

Immediately below this text, I add the following, and successfully save the file using :wq!:

紧接在此文本下方,我添加以下内容,并使用:wq!成功保存文件

 export PATH=/usr/local/bin:$PATH
 export PATH=$PATH:/usr/local/terraform

6. I then again enter the following command to check to see if terraform is detected

6.然后我再次输入以下命令来检查是否检测到terraform

terraform -version

地形版本

Still the same "terraform: command not found" message is returned. I even tried closing out and starting a new command line session and even restarting my computer. Still no change.

仍然返回相同的“terraform:未找到命令”消息。我什至尝试关闭并启动一个新的命令行会话,甚至重新启动我的计算机。还是没有变化。

Anyone have any ideas on how to resolve this? Again, note that I am using "Bash on Ubuntu on Windows" (Linux Subsystem). Any input would be appreciated!

任何人都对如何解决这个问题有任何想法?再次注意,我使用的是“Windows 上的 Ubuntu 上的 Bash”(Linux 子系统)。任何输入将不胜感激!

回答by janos

The direct answer to your problem is at the end. But I think it will make more sense if you keep reading from here.

您的问题的直接答案在最后。但我认为如果你从这里继续阅读会更有意义。

Before trying to add to PATH, I recommend to test a program first. In your case I would do like this:

在尝试添加到 之前PATH,我建议先测试一个程序。在你的情况下,我会这样做:

wget https://releases.hashicorp.com/terraform/0.9.8/terraform_0.9.8_linux_amd64.zip
unzip terraform_0.9.8_linux_amd64.zip
./terraform

Notice the last line ./terraform. The zip file contains a single file, terraform, which now should be in the current directory, so I can run it with ./terraform. If it's executable. If it's not executable then confirm it:

注意最后一行./terraform。zip 文件包含单个文件 ,terraform该文件现在应该位于当前目录中,因此我可以使用./terraform. 如果它是可执行的。如果它不可执行,则确认它:

ls -l terraform

And make it executable if needed:

并在需要时使其可执行:

chmod +x terraform

Now let's add it to PATH. But first, let's decide where to put this executable. /usr/local/binseems a reasonable location. So let's move the terraformexecutable into that directory.

现在让我们将它添加到PATH. 但首先,让我们决定将这个可执行文件放在哪里。 /usr/local/bin似乎是一个合理的位置。因此,让我们将terraform可执行文件移动到该目录中。

Usually /usr/local/binis already on PATH, so you might not need to change anything. Now you can try your check, and there's a good chance it already works:

通常/usr/local/bin已经打开PATH,因此您可能不需要更改任何内容。现在你可以试试你的检查,很有可能它已经起作用了:

terraform -version

If it doesn't, then /usr/local/binis not on the PATH. To add it, add this line in ~/.profile:

如果不是,/usr/local/bin则不在PATH. 要添加它,请将这一行添加到~/.profile

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


Two things looked fundamentally wrong with your approach:

你的方法有两件事看起来根本错误:

  1. Adding /usr/local/terraformto PATH. This is fishy, because the entries on PATHmust be directories, and in your post nothing indicates that you created a directory at /usr/local/terraform.

    • You cdinto /usr/local, and then unzipthe zip file of terraform. The linked zip contains a single file named terraform, so /usr/local/terraformin your example should be a file.
    • If it is a file, then you could make it executable as terraformby adding to add to PATHits base directory. But adding /usr/localto PATHwould not be a good idea. It's conventional to put binaries into /usr/local/bin, not directly into /usr/local
  2. You did not mention how you reloaded ~/.profile. After editing this file, the new commands you added do not get automatically executed in your current shell. They will get executed when you open a new shell. Or you could manually execute the added commands in the current shell.

  1. 添加/usr/local/terraformPATH. 这是可疑的,因为条目PATH必须是目录,并且在您的帖子中没有任何迹象表明您在/usr/local/terraform.

    • cd进入/usr/local,然后unzip是terraform的zip文件。链接的 zip 包含一个名为 的文件terraform,因此/usr/local/terraform在您的示例中应该是一个文件。
    • 如果它是一个文件,那么您可以terraform通过将其添加到PATH其基目录来使其可执行。但是添加/usr/localPATH不是一个好主意。将二进制文件放入/usr/local/bin,而不是直接放入/usr/local
  2. 你没有提到你是如何重新加载的~/.profile。编辑此文件后,您添加的新命令不会在您当前的 shell 中自动执行。当你打开一个新的 shell 时,它们会被执行。或者您可以在当前 shell 中手动执行添加的命令。

回答by user13492635

Hit below command

点击下面的命令

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

导出 PATH=$PATH:/usr/local/bin