1

I am looking for a way to easily pass an environment variable to a .exe when invoked from the Bash on Windows 10 terminal. It seems that

TEST=somevalue example.exe

does not work.

Louis Ritter
  • 11
  • 1
  • 3
  • When you say "Bash", do you mean the Windows command prompt? (it might look like Bash, but it's not quite the same) – JonathanDavidArndt Jul 19 '17 at 03:25
  • @JonathanDavidArndt Windows 10 has a ['Bash on Windows'](https://msdn.microsoft.com/en-us/commandline/wsl/about) where you can run Linux command line tools. – Louis Ritter Jul 19 '17 at 13:12

1 Answers1

0

The reason that the command above doesn't work is because it's a Linux environment variable you're setting. In "normal" Windows cmd.exe, you'd be able to start with set TEST=somevalue but this doesn't appear to work in bash.exe, the Linux environment variable is not shared with the Windows executable (the call to set is actually to the set command in bash itself).

So, if you're looking for a workaround, I can think of a two-step way to accomplish this. First, call the command in the context of cmd.exe instead of bash.exe, which you can do with the /C parameter. Second, you'd need to actually be able to do this in a oneliner in cmd.exe, which also requires a workaround, because doing set FOO="bar" & echo %FOO% does not work in cmd.exe (try it). If you prepend the echo command with call it does. This results in the following full command:

cmd.exe /C "set FOO=\"bar\" & call echo %FOO%"

You'll see that this results in the output "bar".

Jaap Haagmans
  • 414
  • 1
  • 3
  • 11