Run Windows tools from WSL
To make Windows executables easier to run, Windows path is included in the Linux $PATH in Fall Creators Update
piping, redirects, and even backgrounding work as expected
$ ipconfig.exe | grep IPv4 | cut -d: -f2 172.21 10.159
Example using mixed Windows and WSL commands:
$ ls -la | findstr.exe foo.txt
$ cmd.exe /c dir <- contents of C:\ ->
Windows binaries must include the file extension, match the file case, and be executable. Non-executables including batch scripts. CMD native commands like dir can be run with cmd.exe /C command.
Parameters are passed to the Windows binary unmodified.
As an example, the following commands will open C:\temp\foo.txt in notepad.exe:
"" or \
$ notepad.exe "C:\temp\foo.txt" $ notepad.exe C:\\temp\\foo.txt
As an example; wsl.exe is initially launched from C:\temp and the current WSL directory is changed to the user’s home. When notepad.exe is called from the user’s home directory, WSL automatically reverts to C:\temp as the notepad.exe working directory:
C:\temp> wsl c $ cd ~
C:\xxx> wsl ---> /mnt/c/xxx
Share environment variables between Windows and WSL
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. Flags can be combined as needed.
/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.
Disable Interop Users may disable the ability to run Windows binaries for a single WLS session by running the following command as root:
$ echo 0 > /proc/sys/fs/binfmt_misc/WSLInterop
To re-enable Windows binaries either exit all WSL sessions and re-run bash.exe or run the following command as root:
$ echo 1 > /proc/sys/fs/binfmt_misc/WSLInterop
Disabling interop will not persist between WSL sessions -- interop will be enabled again when a new session is launched.
Creators Update and Anniversary Update
bash.exe has been deprecated and replaced with wsl.exe. -c option for running a single command isn't needed with wsl.exe. Windows path is included in the WSL $PATH The process for disabling interop is unchanged.
Invoking WSL from the Windows Command Line Linux binaries can be invoked from the Windows Command Prompt or from PowerShell. Binaries invoked in this way have the following properties:
Use the same working directory as the CMD or PowerShell prompt. Run as the WSL default user. Have the same Windows administrative rights as the calling process and terminal. Example:
C:\temp> bash -c "ls -la"
Linux commands called in this way are handled like any other Windows application. Things such as input, piping, and file redirection work as expected.
C:\temp>bash -c "sudo apt-get update" [sudo] password for username: Hit:1 http://archive.ubuntu.com/ubuntu xenial InRelease Get:2 http://security.ubuntu.com/ubuntu xenial-security InRelease [94.5 kB]
C:\temp> bash -c "ls -la" | findstr foo C:\temp> dir | bash -c "grep foo" C:\temp> bash -c "ls -la" > out.txt
The WSL commands passed into bash -c are forwarded to the WSL process without modification. File paths must be specified in the WSL format and care must be taken to escape relevant characters. Example:
C:\temp> bash -c "ls -la /proc/cpuinfo" -r--r--r-- 1 root root 0 Sep 28 11:28 /proc/cpuinfo C:\temp> bash -c "ls -la \"/mnt/c/Program Files\"" <- contents of C:\Program Files ->
bash -c "ls -la \"/mnt/c/Program Files\""
Invoking Windows binaries from WSL
In WSL, these executables are handled similar to native Linux executables. This means adding directories to the Linux path and piping between commands works as expected. Examples:
$ export PATH=$PATH:/mnt/c/Windows/System32 $ notepad.exe $ ipconfig.exe | grep IPv4 | cut -d: -f2 $ ls -la | findstr.exe foo.txt $ cmd.exe /c dir
The Windows binary must include the file extension, match the file case, and be executable. Non-executables including batch scripts and command like dir can be run with /mnt/c/Windows/System32/cmd.exe /C command.
$ /mnt/c/Windows/System32/cmd.exe /C dir $ /mnt/c/Windows/System32/PING.EXE www.microsoft.com