Bash not executing from command line

12,567

Solution 1

Cause of error

There is a bug with windows thinking legacy console is activated when it is actually unchecked.

Solution

Steps:

  1. Open Command Prompt
  2. Right Click the top bar, and press Properties
  3. Go to options tab, and check Legacy console
  4. Relaunch command Prompt
  5. Go back to the options tab, and uncheck the Legacy console
  6. Relaunch
  7. Windows BUG: The command prompt is most likely a black box with no text
  8. Go to Properties, and then the Terminal tab
  9. Uncheck Use separate foreground and 'Use separate backround'
  10. Now type bash in cmd and it should work flawlessly.

Solution 2

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 takes C:\Windows\System32 from local environment variable PATH to search for a file with name bash with a file extension as defined in local environment variable PATEXT resulting in searching in directory C:\Windows\SysWOW64 for bash. But there is no bash.exe in Windows system directory for 32-bit applications. Therefore no bash.exe is found by 32-bit cmd.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 uses PATH and PATHEXT 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 alias Sysnative which means typing C:\Windows\Sysnative\bash.exe instead of just bash.

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:

mklink /H %SystemRoot%\SysWOW64\bash.exe %SystemRoot%\Sysnative\bash.exe

That creates a hard link with name bash.exe in directory %SystemRoot%\SysWOW64 to file bash.exe in %SystemRoot%\Sysnative. Then 32-bit cmd.exe can find a file bash.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 newer bash.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:

echo @%SystemRoot%\Sysnative\bash.exe %*>%SystemRoot%\SysWOW64\bash.cmd

That command line creates with Windows installed into C:\Windows the batch file C:\Windows\SysWOW64\bash.cmd with the single command line:

@C:\Windows\Sysnative\bash.exe %*

So 32-bit cmd.exe finds now on entering just bash in a 32-bit Windows command prompt window the file bash.cmd in %SystemRoot%\SysWOW64 on using directory path C:\Windows\System32 in local environment variable PATH redirected by file system redirector to C:\Windows\SysWOW64. The batch file runs C:\Windows\System32\bash.exe with all parameters specified on starting bash.cmd passed to bash.exe.

Note: A Windows batch file containing just bash executed by 32-bit Windows command processor results in starting bash.exe via bash.cmd, but then no more command lines in batch file are executed by cmd.exe because of Windows command processor ends batch file execution on finishing execution of bash.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 name bash executed by 32-bit cmd.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.

Share:
12,567
Unloading Gnat
Author by

Unloading Gnat

Hi

Updated on September 18, 2022

Comments

  • Unloading Gnat
    Unloading Gnat over 1 year

    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
    
  • Unloading Gnat
    Unloading Gnat almost 4 years
    This worked for me