113

In cygwin, I could just do ./script.sh args, but this opens the script file in notepad in PowerShell.

What do I need to do have it execute?

wsorenson
  • 5,701
  • 6
  • 32
  • 29
  • 2
    I'm wondering why you expect a *nix-ish script to run in Powershell... They have completely different command sets and scripting syntax (also, powershell scripts require a .ps1 extension AFAIK). You could post the bash script and someone might be able to help you with a powershell version. – John Barrett Jul 08 '09 at 15:37
  • Possible duplicate of [How to run .sh on Windows Command Prompt?](http://stackoverflow.com/questions/26522789/how-to-run-sh-on-windows-command-prompt) – Michael Freidgeim Dec 25 '16 at 16:43

7 Answers7

80

There is now a "native" solution on Windows 10, after enabling Bash on Windows, you can enter Bash shell by typing bash: Bash on Windows

You can run Bash script like bash ./script.sh, but keep in mind that C drive is located at /mnt/c, and external hard drives are not mountable. So you might need to change your script a bit so it is compatible to Windows.

Also, even as root, you can still get permission denied when moving files around in /mnt, but you have your full root power in the / file system.

Also make sure your shell script is formatted with Unix style, or there can be errors. Example script

Marius Tancredi
  • 1,181
  • 1
  • 10
  • 9
  • 1
    @destoryer No it is not a lie. I use it everyday and it works beautifully. Microsoft did do quite a few major changes though, and if you install fresh now, it might be a little bit different. I'm still on a "legacy" installation so mine still looks like what I shown in the answer. Anyway, you can now download Ubuntu from Microsoft Store: https://www.microsoft.com/store/productId/9N9TNGVNDL3Q – Marius Tancredi Nov 28 '18 at 02:18
  • 5
    It does not work in vanilla `PowerShell`. Perhaps, there are som pre-requisites you have not mentioned? – Dr_Zaszuś Jun 21 '20 at 15:36
  • 2
    @Dr_Zaszuś As mentioned in the answer, you first need to enable Bash on Windows. The exact steps to do that have changed over the years, but as of time of this comment, I believe all you need to do is to enable WSL in control panel then download a distro from Microsoft Store. – Marius Tancredi Jun 22 '20 at 18:09
  • If you want to run bash on an external hard drive, just do the simple steps here: https://askubuntu.com/a/1116220/957855 – AlienKevin Oct 29 '20 at 09:09
28

You should put the script as argument for a *NIX shell you run, equivalent to the *NIXish

sh myscriptfile
TheBonsai
  • 15,513
  • 4
  • 22
  • 14
  • 73
    Really? I get The term 'sh' is not recognized as the name of a cmdlet, function, script file, or operable program. – KCD Mar 27 '12 at 00:50
  • 52
    A caveat to the accepted answer is that `sh` is not included in vanilla Powershell. I had to install Git, which optionally adds some UNIX tools to the `PATH` in Powershell, `sh.exe` being one of them. – peplin Nov 09 '12 at 16:56
  • 2
    You can enter NIX shell by typing only `sh` , and there you can run unix/linux commands xD. – IGRACH Apr 27 '15 at 21:23
  • If you have installed git for windows you can use bash.exe xD. – IGRACH Apr 27 '15 at 21:53
  • 72
    Not really an answer since this require you to install something, which isn't mentioned in the answer. – starcorn Dec 15 '16 at 19:59
  • 6
    This does not solve that problem, because you have to install another software to get it working :-(. – Octoate Nov 07 '17 at 19:30
  • I agree, not an answer if it does not mention what step to do first to have sh working. And, yes, I have git installed and it adds no bash or sh commands. – Mohy Eldeen Jun 04 '21 at 22:11
  • 1
    `sh : The term 'sh' is not recognized as the name of a cmdlet, function, script file, or operable program.` – ghost21blade Jan 12 '22 at 10:44
21

If you add the extension .SH to the environment variable PATHEXT, you will be able to run shell scripts from PowerShell by only using the script name with arguments:

PS> .\script.sh args

If you store your scripts in a directory that is included in your PATH environment variable, you can run it from anywhere, and omit the extension and path:

PS> script args

Note: sh.exe or another *nix shell must be associated with the .sh extension.

Rynant
  • 23,153
  • 5
  • 57
  • 71
  • 3
    For non-PowerShell users looking to do this, how does one correctly update the PowerShell `PATH` variable? – Dylan Knowles Apr 20 '16 at 05:59
  • 2
    Powershell uses the same environment variables that all other programs use. If you type "system environment" in the windows start menu, you should see a control panel option come up for editing environment variables. – Rynant Apr 20 '16 at 13:59
18

It also can be run by exporting the bash and sh of gitbash C:\Program Files\git\bin\ to Windows' environmental variables.

In Advance section in the path var kindly add the C:\Program Files\git\bin\ which will make the bash and the sh of the git-bash to be executable from the window cmd.

Restart Powershell and then run the shell file as

bash shellscript.sh or sh shellscript.sh

Mitya
  • 33,629
  • 9
  • 60
  • 107
GangaRam Dewasi
  • 631
  • 7
  • 11
2

Simplest Way (Windows10)

./your_script.sh

But you have to enable script running on PowerShell See Here

ghost21blade
  • 731
  • 2
  • 8
  • 21
  • 1
    For me (who has both WSL2 and Git) this method is far faster than running `bash ./your_script.sh` . I have a lot of files living on Windows that need to be copied to another directory, and your method allows it to occur faster. – Erica Kane Jan 26 '23 at 20:01
1

As ghost21blade suggested, you can just use ./your_script.sh.

Also, you can add “C:\Program Files\Git\bin” to Path in User Environment Variables. In this case you will be able to do sh your_script.sh and bash your_script.sh

Nazar
  • 91
  • 4
0

An addition to the @Marius Tancredi's answer. You can run a bash command from PowerShell with:

bash -c "echo test"

WSL should be configured in Windows.

t7e
  • 322
  • 1
  • 3
  • 9