Sh is not recognized as an internal or external command, operable program or batch file

Sh is not recognized as an internal or external command, operable program or batch file

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

问题描述

由于要下载数据集,需要在Win10下用bash指令。
但是在cmd直接运行,会出现‘bash’ is not recognized as an internal or external command的报错。
网上检索后发现大多都是在Linux下运行的,后来看到这篇文章,完美解决了我的问题。
.sh文件,是shell script格式的,在Linux系统下是可以直接运行的,但是,由于C:\Windows\System32这里是没有bash.exe文件的,在Windows环境下,需要借助第三方软件。

问题解决

bash指令

此处我们使用git,我们先从网上下载GIT。
接着将C:\Program Files\Git\bin加入环境变量中。

编辑环境变量,在path那里加入C:\Program Files\Git\bin。

Sh is not recognized as an internal or external command, operable program or batch file

然后再返回到Command Prompt,就可以成功bash xxx.sh命令了~

wget指令

不过我遇到了新的报错,wget: command not found。也就是找不到这个指令。

Sh is not recognized as an internal or external command, operable program or batch file

所以就需要下载wget指令,这个是Windows的wget指令的下载链接。
下载完后,放到C:\Windows\System32路径下面,然后就可以安静等待数据下载了。

Bug Report

Problem

When I run the following command in PowerShell on Windows 10, I get an error:

cordova build android --verbose

=> 
...
Running command: C:\MyPath\platforms\android\gradlew cdvBuildDebug -b C:\MyPath\platforms\android\build.gradle
'sh' is not recognized as an internal or external command,
operable program or batch file.
Command finished with error code 1: C:\MyPath\platforms\android\gradlew cdvBuildDebug,-b,C:\MyPath\platforms\android\build.gradle
C:\MyPath\platforms\android\gradlew: Command failed with exit code 1 Error output:
'sh' is not recognized as an internal or external command,
operable program or batch file.
Error: C:\MyPath\platforms\android\gradlew: Command failed with exit code 1 Error output:
'sh' is not recognized as an internal or external command,
operable program or batch file.
    at ChildProcess.whenDone (C:\MyPath\node_modules\cordova-common\src\superspawn.js:135:23)
    at ChildProcess.emit (events.js:315:20)
    at ChildProcess.cp.emit (C:\MyPath\node_modules\cross-spawn\lib\enoent.js:34:29)
    at maybeClose (internal/child_process.js:1021:16)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:286:5)

What is expected to happen?

I am able to run this command on Ubuntu (non-WSL) and it successfully finds the correct version of gradlew. However, in Windows, I think it should find gradlew.bat.

What does actually happen?

It runs gradlew on Windows, rather than gradlew.bat

Information

check_reqs.js locates Android Studio and then gradle in this location:

C:\Program Files\Android\Android Studio\gradle\gradle-4.6\bin\gradle

However, ProjectBuilder.js shows this:

build (opts) {
    var wrapper = path.join(this.root, 'gradlew');
    ...

It looks to me like there is no attempt to append .bat to the filename if it’s on Windows. Maybe execa is supposed to append it, but it doesn’t seem to. If I hack this to ‘gradlew.bat’, it builds successfully.

Command or Code

cordova build android --verbose

Environment, Platform, Device

Windows 10

Version information

Cordova 9.0.0 It seems to find Android Studio 3.2.1, although I’m not actually using it.

Checklist

  • I searched for existing GitHub issues
  • I updated all Cordova tooling to most recent version
  • I included all the necessary information above

Accepted answer

I was using windows command prompt to do this. Using the git bash resolved the problem for me. The issue I think was with sh - shell files not being recognized by default in windows. However with git bash, since it by default recognizes the shell scripts it did work perfectly.

As of Nov-2020: By using gitbash versioned code changes to bitbucket. Through Bamboo changes has to be deployed to Salesforce Org. 
Bamboo build fails with the error: "'dynamicBash.sh' is not recognized as an internal or external command, operable program or batch file". Was working fine previously. suddenly getting this error. Please suggest solutions.

Update: 14-june-2021:
Again we are getting the same error for another sandbox of same project.

The Command Prompt in Windows is a handy utility. It allows you to perform many tasks quickly; however, the command processor may show the "not recognized as an internal or external command" error when you try to execute a command.

It is a common error and usually occurs due to incorrect command execution. This article explains the causes and a few troubleshooting steps to help you fix this error.

Why the Not Recognized as an Internal or External Command Error Occurs?

Windows OS keeps a list of paths known as Windows Environment Variables to launch and execute programs quickly. Any issues with it can cause the error.

Potential issues that cause this error include:

  • Incorrect commands.
  • Registry entries interrupting the functioning of commands and more.
  • Executable programs or scripts not installed.
  • An incorrectly specified path or filename.
  • A missing file directory in Windows Environment Variables.

How to Fix the "Not Recognized as an Internal or External Command" Error

This error is often triggered when you run a program or CMD command, and something goes wrong. We have listed fixes for both versions of the error, so follow the relevant one to your case.

1. Verify if the Program Is Installed

control panel installed programs

Command Prompt can’t run a program that does not exist. If you are trying to execute a newly installed program, ensure it is correctly installed.

To verify the installation:

  1. Press Win + R to open Run. Then, type control and click OK.
  2. Then, in the Control Panel, go to Programs > Programs and Features.
  3. Scroll through the list of installed apps and locate the app you want to run.

Alternatively, you can also view installed apps by accessing Apps and Features in Settings. To do this, press Windows key + I, click on Apps and locate the installed app.

By default, when you try to launch a program or a script from CMD, the command processor looks for the related files and paths in the System32 folder or environment variables. If the file is missing, it will return the not recognized as an internal or external command, operable program, or batch file error.

This is likely because Windows does not install most apps in the System32 folder, but C:\Program Files instead.

You can resolve this issue in three ways:

  1. First, use the full path of the executable file to launch the program.
  2. Second, add the program path to Windows environment variables.
  3. Finally, move the files to the System32 folder.

2. Use the Full File Path to Execute the Command

run programs using full file path

First, let's try the full path method. Here, we will use the full file path instead of the app name to launch programs from Command Prompt. Here's how to do it:

  1. First, open the Command Prompt as administrator.
  2. Next, type the full path of the application you want to launch. For example, if you want to open the ESBCalc Port located in the C:\ directory, then the command to open the app with the full path will look something like this:
    C:\ESBCalcPort.exe
  3. That said, this will only work if the file path does not have any white spaces. If your file path has a space, type the file path inside double-quotes.

3. Use the Full File Path Within Double Quotes

launch internet explorer double quotes cmd

Command Prompt reads the white spaces as the end of a command and treats anything after the space as a separate command.

Let’s say you want to launch the Internet Explorer app located in C:\Program Files\Internet Explorer\iexplore.exe via CMD. However, running this file path will return an error due to the space between Program Files and Internet Explorer.

To avoid this, you can insert the file path within a double quote. Here, the command to launch Internet Explorer will look like this:

"C:\Program Files\Internet Explorer\iexplore.exe"

4. Add the File Path to the Windows Environment Variables

You can edit the Environment Variables and add the file path of your application to its directory. This way, Command Prompt will identify the path for the entered command and execute it without the error.

Adding a full file path to the Windows Environment Variable can help you resolve the not recognized as an internal or external command error for the Flex, nmake, make, cobra, Is, terraform, gcc, code, Android Studio, Python, Fastboot, and ADB commands.

To edit Windows Environment Variable:

  1. Press Win + R to open Run.
  2. Next, type control and click OK to open the Control Panel.
  3. In Windows 10, Go to System and Security > System. In the left pane, click on Advanced System Settings.
  4. On newer versions of Windows 10 and Windows 11, go to Settings > System > About. Then, click on Advanced system settings underthe Related settings section.
  5. In the Advanced tab, click on the Environment Variables button.

    edit windows environment variable path

  6. In the new window, under System variables, select the Path variable.
  7. Click the Edit button.
  8. In the Edit window, click on New.

    add file path environment variable

  9. Here, you have to paste the program's file path you want to run from CMD. For example, if you want to run the Chrome browser located in C:\Program Files\Google\Chrome\Application, then the full file path will look like this:
    C:\Program Files\Google\Chrome\Application
  10. Click OK to add it to the Environment Variables.
  11. Then, click OK again.
  12. Next click OK and Apply to save the changes.

Once you have added the new environment variable, restart your PC to apply the changes. After the PC restarts, open Command Prompt, enter the program's name, and CMD will open it without any error.

If you experience issues with the commands like ipconfig, netsh, cipher, etc., then add the Windows system root folder file path in the Environment Variable dialog. Here's how to do it.

  1. Open the Environmental Variables dialog.

    SystemPropertiesAdvanced add systemroot folder path environmental variableJ

  2. Next, select the Path variable under the System variables section and click on Edit.
  3. Click New and paste the following:
    %SystemRoot%
    ystem32
  4. Click OK to save the changes.
  5. Click OK, and then click OK and Apply to save the changes.

5. Move Files to System32 Folder

move chrome files system32 folder

System32 is a protected system folder, and it is protected for a good reason. When you try to launch a program as an administrator, Command Prompt looks for the executable file in the System32 folder. However, since all the programs are not installed in the System32 folder, you may end up with an error.

If you still want to run the program from CMD and don’t want to type a full file path, you can move the program files to the System32 folder.

To move program files to the System32 folder:

  1. Navigate to the installation directory for your program and copy all the files in the folder. Here, we will move Google Chrome files located in C:\Program Files\Google\Chrome\Application to the System32 folder.
  2. Next, navigate to the C:\Windows\System32 folder and paste the copied files. Click Yes if a UAC prompt appears.
  3. Next, open the Command Prompt as administrator, type chrome, and hit enter. CMD will instantly open the Google Chrome browser.

Fixing the CMD Not Recognized as an Internal or External Command Error

Command Prompt commands are not case-sensitive, but it does not forgive the use of extra spaces. To resolve this error, check if the commands are correct and use file paths with spaces within double-quotes. For the programs not installed in the System32 folder, add an environment variable with your application’s full file path to launch apps through CMD.

How do you fix CMD is not recognized as an internal or external command?

You can resolve this issue in three ways: First, use the full path of the executable file to launch the program. Second, add the program path to Windows environment variables. Finally, move the files to the System32 folder.

How do I run a .sh file in Command Prompt?

Execute Shell Script Files Open Command Prompt and navigate to the folder where the script file is available. Type Bash script-filename.sh and hit the enter key. It will execute the script, and depending on the file, you should see an output.

How do I make a .sh file executable in Windows?

Make a Bash Script Executable.
1) Create a new text file with a . sh extension. ... .
2) Add #!/bin/bash to the top of it. This is necessary for the “make it executable” part..
3) Add lines that you'd normally type at the command line. ... .
4) At the command line, run chmod u+x YourScriptFileName.sh. ... .
5) Run it whenever you need!.

Can we run .sh file in Windows?

A . SH file is very similar to the batch file of the Windows operating system and can be run in the Linux-based operating system. It is also possible to run . SH file in Windows 10 using Windows Subsystem for Linux.