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

Viewed 31095

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

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

echo %JAVA_HOME%

i got

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

I want to import the path data to bash

enter image description here

5 Answers

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

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

    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 .sh file and execute it using 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. But you can run windows programs from Ubuntu on 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). But below should work. run is documented to run windows programs from the bash shell it gives you.

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.

System variables

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

e.g.

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