141

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?

2
  • 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. Commented Jul 8, 2009 at 15:37
  • Possible duplicate of How to run .sh on Windows Command Prompt? Commented Dec 25, 2016 at 16:43

8 Answers 8

90

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

Sign up to request clarification or add additional context in comments.

5 Comments

@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: microsoft.com/store/productId/9N9TNGVNDL3Q
It does not work in vanilla PowerShell. Perhaps, there are som pre-requisites you have not mentioned?
@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.
If you want to run bash on an external hard drive, just do the simple steps here: askubuntu.com/a/1116220/957855
This is misleading, the resulting "bash shell" isn't running in true Windows context, it is running under a Ubuntu WSL. You can confirm this by typing cat /etc/lsb-release once inside the bash shell. So depending what you are doing, this may or may not be what you are looking for.
33

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

sh myscriptfile

8 Comments

Really? I get The term 'sh' is not recognized as the name of a cmdlet, function, script file, or operable program.
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.
You can enter NIX shell by typing only sh , and there you can run unix/linux commands xD.
Not really an answer since this require you to install something, which isn't mentioned in the answer.
This does not solve that problem, because you have to install another software to get it working :-(.
|
28

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

1 Comment

Finally, something that worked!
23

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.

3 Comments

For non-PowerShell users looking to do this, how does one correctly update the PowerShell PATH variable?
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.
It doesn't work. It still opens file editor for me. I restarted the terminal.
4

Simplest Way (Windows10)

./your_script.sh

But you have to enable script running on PowerShell See Here

1 Comment

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.
4

Using Git bash

This is a variant of some of the other answers. It results in having your *.sh associated with the bash shell installed with Git, but without the hassle of having a Unix/Linux installation as with WSL.

It is common to have git as version control system nowadays. It is also quite common to use the git client provided by the git site. If you accept associating shell scripts to git bash during installation, you will not need any extra step: you already can run bash scripts from PowerShell.

When installing git, make sure to clear the checkbox "Only show new options" to have full control over the installation.

Show all options while installing Git

Then be sure to mark the checkmark "Associate .sh files to be run with Bash".

Associate Bash to run .sh files

You are set.

Fom now on, you can run Bash sell scripts the same way as you run any other kind of file that has an executable associated:

  • from a command line box (cmd, PowerSell)
  • from the run box (Win+R)
  • Windows file explorer
  • etc.

Comments

3

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: https://learn.microsoft.com/en-us/windows/wsl/install

2 Comments

I get execvpe /bin/bash failed 2 and create process not expected to return, this answer does not work, or you need more explanation on how to configure wsl
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.