wsorenson
wsorenson

Reputation: 5971

Run bash script from Windows PowerShell

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?

Upvotes: 135

Views: 481800

Answers (8)

t7e
t7e

Reputation: 509

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

Upvotes: 2

manuelvigarcia
manuelvigarcia

Reputation: 2104

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.

Upvotes: 4

GangaRam Dewasi
GangaRam Dewasi

Reputation: 741

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

Upvotes: 28

Nazar
Nazar

Reputation: 340

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

Upvotes: 1

ghost21blade
ghost21blade

Reputation: 613

Simplest Way (Windows10)

./your_script.sh

But you have to enable script running on PowerShell See Here

Upvotes: 4

Marius Tancredi
Marius Tancredi

Reputation: 1287

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

Upvotes: 86

Rynant
Rynant

Reputation: 24343

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.

Upvotes: 23

TheBonsai
TheBonsai

Reputation: 16555

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

sh myscriptfile

Upvotes: 31

Related Questions