Can I create a symlink to a Windows executable in the Windows Subsystem for Linux (WSL) PATH?

8,068

Solution 1

You need to prefix the executable with its path. Alternatively you can modify the Linux PATH environment variable and append the Windows path (prefixed with /mnt/c/).

From: Windows Subsystem for Linux interoperability with Windows

Invoking Windows binaries from WSL

The Windows Subsystem for Linux can invoke Windows binaries directly from the WSL command line. Applications run this way have the following properties:

  1. Retain the working directory as the WSL command prompt except in the scenario explained below.
  2. Have the same permission rights as the bash.exe process.
  3. Run as the active Windows user.
  4. Appear in the Windows Task Manager as if directly executed from the CMD prompt.

Example:

$ /mnt/c/Windows/System32/notepad.exe

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.

Examples:

$ /mnt/c/Windows/System32/cmd.exe /C dir
$ /mnt/c/Windows/System32/PING.EXE www.microsoft.com

Parameters are passed to the Windows binary unmodified.

Even in Linux symbolic links contain the path. Take for example this command:

$ ls -la /bin | grep ^l
(...SNIP...)
lrwxrwxrwx  1 root root      20 Jun 27 09:49 systemd -> /lib/systemd

systemd is linked to /lib/systemd/systemd. So your symbolic link of:

sudo ln -s -t java.exe java

I imagine (because I'm not a link expert) would need to look something like:

sudo ln -s -t /mnt/c/Windows/Path/To/java.exe java

Use alias instead of symbolic link

Rather than a symbolic link I would create an alias in my ~/.bashrc file thusly:

alias java='/mnt/c/WindowsPathTo/java.exe'

Then whenever you type java the mumbo-jumbo is typed on your behalf. Any parameters passed are honoured.

Solution 2

I'm using Fish-Shell and found that both alias and symbolic links don't really work. I'm not sure if it's an issue with fish though, I rather suspect Windows' weird path naming is the problem.

My solution was to add a function to fish.config (the equivalent of .bashrc)

function npp
    "/mnt/c/Program Files (x86)/Notepad++/notepad++.exe" $argv
end

($argv in fish is the equivalent to $* in bash)

Then I can call Notepad++ via: npp test.txt

Share:
8,068

Related videos on Youtube

Uche Ozoemena
Author by

Uche Ozoemena

Updated on September 18, 2022

Comments

  • Uche Ozoemena
    Uche Ozoemena over 1 year

    I have Android Studio and JDK 8 installed on Windows, but the JDK cli isn't available on my WSL. When I type java or javac, the command isn't found. I followed the suggestion here and now I can reach the commands from WSL by typing java.exe and javac.exe, but I don't want to have to specify the .exe extension. My idea is to add a symlink that points to the executables, but without having to specify the path to the executable (/mnt/c/...) seeing as that's already been done in the WSL PATH. So can I create a symlink that points to a location in the PATH? If this doesn't make sense, this is what I tried:

    sudo ln -s -t java.exe java
    

    and I got "failed to access 'java.exe': No such file or directory". Yet this is exactly what I want to achieve: a symlink to an executable in the PATH without specifying the actual path to the executable because PATH already has that. If I'm crazy and this is silly/impossible/unnecessary, you can say that as the answer and I'll accept it! :-D

  • Fabby
    Fabby over 4 years
    The alias should be first, but have a +1 anyway... ;-)
  • Uche Ozoemena
    Uche Ozoemena over 4 years
    Rather late but @WinEunuuchs2Unix thanks for your great and detailed answer! I ended up using the alias method. For anyone else wondering why it works, aliases apparently have higher priority than symlinks in bash, as explained in this answer: unix.stackexchange.com/a/132628/363992.
  • Vadzim
    Vadzim over 4 years
    Aliases are hardly compatible with shell scripts. So here is my solution with helper script for quick symlink creation: stackoverflow.com/questions/45185086/…
  • Uche Ozoemena
    Uche Ozoemena over 4 years
    @Vadzim thanks for that! Ultimately I have decided to install the Linux versions of any executable, where possible. I can’t always predict which windows executable will work properly in WSL and which ones will throw unexpected errors due to incompatibility.