John
John

Reputation: 1819

How do you pass an environment variable from WSL into windows executable

From a Windows Subsystem for Linux (v1) Alpine bash terminal, I would like to set an environment variable that get's passed into a windows executable. Is there any way to do this?

example of what I was hoping would print "Hello, World!":

windows-10:~# export X=World
windows-10:~# cmd.exe /c 'echo Hello, %X%!'
Hello, %X%!

See answer from Philipe below.

Here is a copy of the pertinent info from https://learn.microsoft.com/en-us/windows/wsl/interop

Share environment variables between Windows and WSL

Available in Windows Insider builds 17063 and later.

Prior to 17063, only Windows environment variable that WSL could access was PATH (so you could launch Win32 executables from under WSL).

Starting in 17063, WSL and Windows share WSLENV, a special environment variable created to bridge Windows and Linux distros running on WSL.

Properties of WSLENV:

It is shared; it exists in both Windows and WSL environments.
It is a list of environment variables to share between Windows and WSL.
It can format environment variables to work well in Windows and WSL.

There are four flags available in WSLENV to influence how that environment variable is translated.

WSLENV flags:

/p - translates the path between WSL/Linux style paths and Win32 paths.
/l - indicates the environment variable is a list of paths.
/u - indicates that this environment variable should only be included when running WSL from Win32.
/w - indicates that this environment variable should only be included when running Win32 from WSL.

Flags can be combined as needed.

Upvotes: 9

Views: 8409

Answers (1)

Philippe
Philippe

Reputation: 26527

Can you try this ?

~$ export X=World
~$ export WSLENV=X/w
~$ cmd.exe /c 'echo Hello, %X%!'
Hello, World!

Upvotes: 9

Related Questions