General Computing
windows bash path cygwin windows-subsystem-for-linux
Updated Sun, 11 Sep 2022 10:28:39 GMT

Windows: How to make cygwin bash.exe to take precedence over wsl in command line?


I have installed cygwin on Windows 11. Now, when I open the Windows command line, I want the bash.exe command to run the cygwin's bash command instead of bash linked with wsl (Windows subsystem for Linux). In other words, when I run bash.exe I don't want to see the "Windows Subsystem for Linux has no installed distributions." message, but instead run bash from my cygwin installation. I have tried adding the cygwin's "bin" directory to the Windows PATH environment variable, but with no effect.

Edit

Originally, I wrote that adding the cygwin's "bin" folder path to the Windows PATH environment variable had no effect, and while it is true for running bash-exe from the Windows cmd shell, it has meanwhile actually solved the problem of running bash.exe from the Python script, which in reality was my problem. I thought that running the command behaved equally no matter whether run from the cmd shell or Python script, but obviously there is a difference.




Solution

When you say "command line", I'm going to assume you mean the CMD shell, although you could potentially mean PowerShell.

For CMD, the easiest way to do this is probably with a doskey alias for bash:

doskey bash=D:\cygwin\bin\bash.exe $*

To make this permanent, see this answer.

For PowerShell, use the New-Alias equivalent and set it in your PowerShell profile.


Other options:

Windows Terminal

However, you may want to consider two other tools to make this even (IMHO) easier:

Both of these are Microsoft products and installable from the Microsoft Store.

You can set up a Cygin profile in Windows Terminal named "Bash", and using PowerToys run, activate it via _Bash. The prefix for any Terminal profile is _, but you can even skip that if you use it often. Power Run will sort it to the top based on usage.

I recommend turning on the option Open profiles in a new tab in PowerToys Run's "Windows Terminal" settings.

Adjust path

You could adjust your path, but you'll need to do that in the current CMD or PowerShell session, so you'd still need a startup CMD script or PowerShell profile. Given that, I think the Alias approach is probably better.





Comments (1)

  • +0 – Thanks. Actuallly, the 'bash.exe' command was used in some Python script which I cannot modify, not in cmd. However, looks like adding the path to the cygwin's bin folder to PATH in Windows solved the problem. — Jul 23, 2022 at 09:22