Description:
I am not able to execute bash
from my command line opened from desktop. I have to cd
to system32
to run it.
Error message:
'bash' is not recognized as an internal or external command,
operable program or batch file.
Expected results:
After I enter bash
in the cmd
, the Windows Subsystem for Linux opens in the current directory.
Extra:
I have enabled Windows Subsystem for Linux, and bash
works fine on the Ubuntu app. I think there is a PATH
issue, but not sure how to fix it.
Windows Developer Mode is enabled. Legacy Console is unchecked.
System environment variables with their values:
ComSpec C:\Windows\system32\cmd.exe
DriverData C:\Windows\System32\Drivers\DriverData
NUMBER OF_PROCESSORS 8
OS Windows_NT
Path C:\Program Files (x86)\Microsoft SDKs\Azure\CLI2\wbin;C:\Pr...
PATHEXT .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
PROCESSOR_ARCHITECTURE AMD64
PROCESSOR_IDENTIFIER Intel64 Family 6 Model 142 Stepping 12, GenuineIntel
PROCESSOR_LEVEL 6
PROCESSOR_REVISION 8e0c
PSModulePath %ProgramFiles%\WindowsPowerShell\Modules;C:\Windows\s...
TEMP C:\Windows\TEMP
TMP C:\Windows\TEMP
USERNAME SYSTEM
windir C:\Windows
Search results in System32
bash.exe.mui Date modified: 2020-07-03 7:33 PM
C:\Windows\System32\en-US Type: MUI File Size: 4.50 KB
bash.exe Date modified: 2020-01-01 10:10 PM
C:\Windows\System32 Type: Application Size: 118 KB
Best Answer
Windows Subsystem for Linux
There is used currently Windows 10 1903 according to Windows update history records. My first recommendation is to upgrade to currently latest Windows 10 which is Windows 10 2004 using the Microsoft page Download Windows 10 if that is possible. There have been lots of improvements on Windows Subsystem for Linux (WSL) like introducing Windows Subsystem for Linux 2 as it can be read for example on Wikipedia page Windows 10 version history for Version 2004 (May 2020 Update). The Windows Developer Mode should be disabled before starting the Windows 10 upgrade.
Cause of the error
The error is caused by starting 32-bit Windows command processor in
%SystemRoot%\SysWOW64\cmd.exe
instead of 64-bit version in%SystemRoot%\System\cmd.exe
. For that reason the Windows File System Redirector is active when 32-bit Windows command processor takesC:\Windows\System32
from local environment variablePATH
to search for a file with namebash
with a file extension as defined in local environment variablePATEXT
resulting in searching in directoryC:\Windows\SysWOW64
forbash
. But there is nobash.exe
in Windows system directory for 32-bit applications. Therefore nobash.exe
is found by 32-bitcmd.exe
.Please read on Stack Overflow What is the reason for "X is not recognized as an internal or external command, operable program or batch file"? for more details about how
cmd.exe
usesPATH
andPATHEXT
and how these two very important environment variables can be configured by a user.Solution 1: Using 64-bit Windows command processor
The first solution is opening a 64-bit command prompt by starting
%SystemRoot%\System\cmd.exe
instead of%SystemRoot%\SysWOW64\cmd.exe
on 64-bit Windows.This is definitely the best and easiest to achieve solution.
Solution 2: Run BASH with full qualified file name
The second solution is running
bash
from within 32-bit command prompt window with full qualified file name using the special aliasSysnative
which means typingC:\Windows\Sysnative\bash.exe
instead of justbash
.See also the Microsoft documentation Naming Files, Paths, and Namespaces.
This is definitely the best solution on using already the 32-bit Windows environment.
Solution 3: Hard link to bash.exe
Attention: This solution is not recommended for long time usage!
The third solution is starting
%SystemRoot%\SysWOW64\cmd.exe
as administrator with clicking with secondary (right) mouse button on this file in Windows File Explorer and clicking on context menu item Run as administrator to explicitly start 32-bit Windows command processor with elevated privileges of local administrator.Next enter following command line:
That creates a hard link with name
bash.exe
in directory%SystemRoot%\SysWOW64
to filebash.exe
in%SystemRoot%\Sysnative
. Then 32-bitcmd.exe
can find a filebash.exe
in%SystemRoot%\SysWOW64
which results in execution of%SystemRoot%\System32\bash.exe
in Windows system directory for 64-bit applications.Run in command prompt window
mklink /?
for help on this command or read even better SS64 documentation of MKLINK.Note: It is advisable to delete that hard link with
del %SystemRoot%\SysWOW64\bash.exe
executed in a command prompt window opened with Run as administrator before doing a Windows update or Windows upgrade which updates%SystemRoot%\System32\bash.exe
and recreate the hard link after newerbash.exe
is installed by the Windows update/upgrade.I don't recommend using this solution as it could cause easily troubles in future.
Solution 4: Creation of bash.cmd
Attention: This solution is not recommended for compatibility with batch files containing
bash
!The fourth solution is starting
%SystemRoot%\SysWOW64\cmd.exe
as administrator with clicking with secondary (right) mouse button on this file in Windows File Explorer and clicking on context menu item Run as administrator to explicitly start 32-bit Windows command processor with elevated privileges of local administrator.Next enter following command line:
That command line creates with Windows installed into
C:\Windows
the batch fileC:\Windows\SysWOW64\bash.cmd
with the single command line:So 32-bit
cmd.exe
finds now on entering justbash
in a 32-bit Windows command prompt window the filebash.cmd
in%SystemRoot%\SysWOW64
on using directory pathC:\Windows\System32
in local environment variablePATH
redirected by file system redirector toC:\Windows\SysWOW64
. The batch file runsC:\Windows\System32\bash.exe
with all parameters specified on startingbash.cmd
passed tobash.exe
.Note: A Windows batch file containing just
bash
executed by 32-bit Windows command processor results in startingbash.exe
viabash.cmd
, but then no more command lines in batch file are executed bycmd.exe
because of Windows command processor ends batch file execution on finishing execution ofbash.cmd
executed without usage of command CALL from initially executed batch file. So this solution is not safe to use for batch files containing just the file namebash
executed by 32-bitcmd.exe
.I don't recommend using this solution as it could cause troubles with batch files.
Conclusion
There are definitely even more solutions, but best is running 64-bit Windows command processor on 64-bit Windows to have unrestricted access to all executables ported from Linux and installed by default with Windows 10 like
bash.exe
or the executables in directory%SystemRoot%\System32\OpenSSH
which exist also only as 64-bit executables.