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