如何从 [windows path] 设置 [Bash on Ubuntu on Windows] [环境变量]?

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

How to set [Bash on Ubuntu on Windows] [environment variables] from [windows path]?

bashubuntucommand-linewindows-10windows-subsystem-for-linux

提问by Mohamed Elrashid

Try to use samza.apache.org/startup/hello-samza/0.7.0/with Bash On Windows

尝试在 Windows上使用samza.apache.org/startup/hello-samza/0.7.0/和 Bash

it will run

它会运行

bin/grid bootstrap

where the flowing code

流动的代码在哪里

if [ -z "$JAVA_HOME" ]; then
  if [ -x /usr/libexec/java_home ]; then
    export JAVA_HOME="$(/usr/libexec/java_home)"
  else
    echo "JAVA_HOME not set. Exiting."
    exit 1
  fi
fi

give an error

报错

JAVA_HOME not set. Exiting.

on CMD when i run

当我运行时在 CMD 上

echo %JAVA_HOME%

i got

我有

C:\Program Files (x86)\Java\jdk1.8.0_102\

I want to import the path data to bash

我想将路径数据导入到 bash

enter image description here

在此处输入图片说明

回答by Shahein Moussavi

I would try export JAVA_HOME="/mnt/c/Program Files (x86)/Java/jdk1.8.0_102"to set the JAVA_HOME variable in the bash shell.

我会尝试export JAVA_HOME="/mnt/c/Program Files (x86)/Java/jdk1.8.0_102"在 bash shell 中设置 JAVA_HOME 变量。

Update (response to your edit):

更新(对您的编辑的回应):

I wouldn't recommend trying to automatically import your Windows paths to Bash on Ubuntu on Windows, because the paths have to be converted to be understood by the bash shell (\to /, C:\to mnt/c/and so on), and because not all of the tools you're probably going to reference will work on both Windows and Linux. Instead, install what you need on the Bash shell using apt-get(you don't need to use sudobecause BUW loads in a root shell). Java is probably fine to reference as above, but most things you'll want installed separately on Ubuntu.

我不建议尝试在 Windows 上的 Ubuntu 上自动将您的 Windows 路径导入到 Bash,因为必须转换路径才能被 bash shell(\to /C:\tomnt/c/等)理解,并且因为并非所有工具都可以' 可能会参考适用于 Windows 和 Linux。相反,使用 Bash shell 安装您需要的东西apt-get(您不需要使用,sudo因为 BUW 在根 shell 中加载)。如上所述,Java 可能很适合参考,但是您需要在 Ubuntu 上单独安装大多数东西。

回答by raghav710

As a quick solution, I created a powershell script that would

作为一个快速的解决方案,我创建了一个 powershell 脚本,它可以

  1. Take all windows environment variables
  2. Change the path separator to /
  3. Change C:to /mnt/c
  4. Output exportcommands one line per environment variable

    Get-ChildItem Env: | % {"export $($_.Name)=`"$($_.Value.Replace('\', '/').Replace('C:', '/mnt/c'))`""}
    
  1. 获取所有 Windows 环境变量
  2. 将路径分隔符更改为 /
  3. 更改C:/mnt/c
  4. export每个环境变量输出一行命令

    Get-ChildItem Env: | % {"export $($_.Name)=`"$($_.Value.Replace('\', '/').Replace('C:', '/mnt/c'))`""}
    

Now, all you need to do is run this script in Powershell, copy the output and paste it in WSL/Ubuntu on Windows to populate the environment variables. You could also put all these commands in a .shfile and execute it using bash.

现在,您需要做的就是在 Powershell 中运行此脚本,复制输出并将其粘贴到 Windows 上的 WSL/Ubuntu 中以填充环境变量。您也可以将所有这些命令放在一个.sh文件中并使用 bash 执行它。

It is a crude solution, but this worked for me. I'm open to suggestions on improving this.

这是一个粗略的解决方案,但这对我有用。我愿意接受关于改进这一点的建议。

回答by TamusJRoyce

My path environment variable seems to already have my windows paths there. But you can run windows programs from Ubuntu on Windows. So you can get environment variables or whatever you like.

我的路径环境变量似乎已经有我的 Windows 路径。但是您可以在 Windows 上从 Ubuntu 运行 Windows 程序。所以你可以得到环境变量或任何你喜欢的东西。

export PATH=$PATH:`/mnt/c/Windows/System32/cmd.exe -/C "echo $PATH"`

It isn't recommended to use Cygwin (licensing, registry corruption, etc). But below should work. run is documented to run windows programs from the bash shell it gives you.

不建议使用 Cygwin(许可、注册表损坏等)。但下面应该工作。run 被记录为从它给你的 bash shell 运行 Windows 程序。

export PATH=$PATH:`run /mnt/c/Windows/System32/cmd.exe -/C "echo $PATH"`