简体   繁体   English

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

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

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

在此处输入图像描述

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.我不建议在 Windows 上的 Ubuntu 上尝试自动将 Windows 路径导入 Bash,因为必须转换路径才能被 bash shell 理解( \\/C:\\mnt/c/等等) ,并且因为并非您可能要参考的所有工具都适用于 Windows 和 Linux。 Instead, install what you need on the Bash shell using apt-get (you don't need to use sudo because BUW loads in a root shell).相反,使用apt-get在 Bash shell 上安装你需要的东西(你不需要使用sudo因为 BUW 在 root shell 中加载)。 Java is probably fine to reference as above, but most things you'll want installed separately on Ubuntu.如上所述,Java 可能很适合参考,但是您需要在 Ubuntu 上单独安装大多数东西。

As a quick solution, I created a powershell script that would作为一个快速的解决方案,我创建了一个 powershell 脚本,它可以

  1. Take all windows environment variables获取所有 Windows 环境变量
  2. Change the path separator to /将路径分隔符更改为/
  3. Change C: to /mnt/cC:更改为/mnt/c
  4. Output export commands one line per environment variable每个环境变量一行输出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.现在,您需要做的就是在 Powershell 中运行此脚本,复制输出并将其粘贴到 Windows 上的 WSL/Ubuntu 中以填充环境变量。 You could also put all these commands in a .sh file and execute it using bash.您还可以将所有这些命令放在一个.sh文件中并使用 bash 执行它。

It is a crude solution, but this worked for me.这是一个粗略的解决方案,但这对我有用。 I'm open to suggestions on improving this.我愿意接受关于改进这一点的建议。

My path environment variable seems to already have my windows paths there.我的路径环境变量似乎已经有我的 Windows 路径。 But you can run windows programs from Ubuntu on Windows.但是您可以在 Windows 上从 Ubuntu 运行 Windows 程序。 So you can get environment variables or whatever you like.所以你可以得到环境变量或任何你喜欢的东西。

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

It isn't recommended to use Cygwin (licensing, registry corruption, etc).不建议使用 Cygwin(许可、注册表损坏等)。 But below should work.但下面应该工作。 run is documented to run windows programs from the bash shell it gives you. run 被记录为从它给你的 bash shell 运行 Windows 程序。

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

This one is working for me, by setting WSLENV in the System variables environment.通过在系统变量环境中设置WSLENV ,这个对我有用

System variables系统变量

export [variable-name]='any-path' for current shell session export [variable-name]='any-path' for current shell session

eg例如

export VSCode='C:/Program Files/....'
echo $VSCODE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows中的Bash终端(Git bash)是否有刷新环境变量的命令? - Is there a command to refresh environment variables from the Bash Terminal (Git bash) in Windows? 如何从Windows的Ubuntu bash访问ruby - How to access ruby from Ubuntu bash for windows 如何在Windows 10环境中从Java运行在Ubuntu上用Bash编写的Shell脚本? - How can I run a shell script, written in Bash on Ubuntu, from Java in a Windows 10 environment? npm二进制+ Windows git bash +环境变量 - npm binary + windows git bash + environment variables 如何在Ubuntu和Windows(Cygwin)中使用的bash脚本中设置Java类路径 - How to set Java classpath in bash script used in Ubuntu and Windows (Cygwin) 如何从Bash到Windows上获取windows路径(相当于cygpath)? - How to get the windows path from in Bash On Windows (equivalent to cygpath)? 如何使用bash从Windows PATH中提取指定的路径 - How to extract specified path from Windows PATH using bash Android Studio:如何从Windows 10 Ubuntu bash集成Git? - Android Studio: How to integrate Git from Windows 10 Ubuntu bash? 如何从 WSL (Ubuntu) Bash 运行 Windows 可执行文件 - How can I run a Windows executable from WSL (Ubuntu) Bash 如何从脚本中设置bash环境变量? - How do I set bash environment variables from a script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM