BASH on Ubuntu on Windows (AKA Linux Subsystem for Windows) 10 - How do I set my PATH?

Update Summary

The executables in question are not linux native format (ELF), they are compiled for windows. During path expansion, bash checks the magic number of the binary, if it's not a match for ELF, it doesn't expose it via path expansion. However, bash for windows did include a capability to launch native windows applications from the bash environment, which is why direct execution (without path expansion and subsequent binary check) works fine.

Resolution is either an alias based .bashrc addition (or any number of alternative methods to mimic path expansion thus bypassing bash file evaluation) or installation of the linux version.


Original answer

File Permissions

It's possibly a cross file system permissions issue. If you cd /mnt/c/Program\ Files/apache-maven-3.3.9/bin and try to run mvn like so ./mvn what happens?

What is the output of ls -alt in that directory?

If the file isn't properly marked as executable, it won't show up as a 'program' on your path. If it's a binary file and not in the 'linux' format (ELF) then it will also not show up as a path executable.

If directly executing mvn doesn't work (please post the ls results), try adding execute permissions chmod ug+x mvn

Wrong version installed

Are you sure you have the linux native version installed - the same version you used with cygwin will almost definitely not work.

You can check binary compatibility with sudo apt-get install elf-binutils then on the file mvn use the command readelf -a mvn If you get an error such as 'Not an ELF file...` then you have your answer.

I just noticed that you didn't add execute permissions to the test shell script in your example, which (unless you simply forgot to list the step), completely explains that particular failure.

Summary:

  1. Make sure the executables in the maven bin directory are properly set as executable by using chmod. Post the output of ls -alt in your answer.
  2. Make sure you have a linux binary - use the utilty readelf to verify.
  3. Run your shell script test again, but this time mark the file executable.

Update

The path issue was a red herring; you are just trying to execute a binary format that is not compatible with the linux on windows environment.

On the surface the two environments (cygwin and bash on windows) provide a somewhat similar user experience but the implementation and resulting binary compatibility are very different.

Bottom line - Cygwin and Linux binary formats are not compatible. You need to install the linux native version to run it from bash on windows. You could also compile it from source inside the bash on windows environment; but due to the 'early days' nature of the environment I'd worry about chasing dependencies.

Brief description of the two environments:

Cygwin is effectively a translation layer that provides an API for system calls that are typically not available on non-POSIX systems, which allows you to compile many programs written to run on linux in the windows environment. However it is still running in a 'windows' environment - that binary will now only operate in the cygwin environment on windows. This translation layer and associated libraries allow source code written against the Linux API to be compiled in the cygwin environment and run on windows. Binaries built in this manner will not run on Linux or Windows natively; only in the cygwin environment.

The bash environment on windows provided by canonical is much different than cygwin. It actual 'recreates' an environment for a program that appears to actually be linux - i.e. the standard libraries are available along with POSIX system calls - without requiring any modification to the binaries. In many cases a binary built against ubuntu can be copied directly to the bash on windows environment and run without any issues.

To be recognized as a valid executable in bash on windows it needs to be in a native linux binary format or a script file marked with the program to interpret it (for a bash script, #!/bin/bash). A native linux binary will have been built against linux libraries and system calls. Bash confirms that something is a valid executable both by checking the executable permission bits and checking the binary file format is compatible (a 'magic number' check). If it's a binary file and isn't in ELF format, it isn't exposed to the shell via path expansion.

To make this issue harder to clarify, they have added partial ability to launch native windows applications from bash on windows, but clearly didn't address the bash path expansion binary file format check - or they did and it's a bug.

Second edit:

Clarification on your question:

When you launch it directly (./mvn) it bypasses the Bash evaluation and just executes it. The bash on windows environment is smart enough to launch windows native executables, which that must be. I don't believe a cygwin binary would launch properly from bash, but I could be incorrect - documentation is meager at this point and I don't have a test environment accessible right now.

Workaround that provides equivalent capability to 'path' support:

If you are otherwise entirely happy with the maven installation (no other compatability issues, everything just 'works'), but having it on the path is important, you can use a simple workaround which will provide equivalent capability.

In your .bashrc file, add the following alias:

alias mvn='/mnt/c/Program\ Files/apache-maven-3.3.9/bin/mvn'

Repeat the equivalent for any other executables in that directory you want access to from anywhere within the bash on windows environment.

restart bash or source the file, and then mvn will work from any directory (based on your statement that direct execution from within the bin dir, ./mvn, was working).