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.

  • When you say "Bash", do you mean the Windows command prompt? (it might look like Bash, but it's not quite the same) Jul 19 '17 at 3:25
  • @JonathanDavidArndt Windows 10 has a 'Bash on Windows' where you can run Linux command line tools. Jul 19 '17 at 13:12

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".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.