Run bash script from Windows PowerShell

BashPowershell

Bash Problem Overview


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?

Bash Solutions


Solution 1 - Bash

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

Solution 2 - Bash

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

sh myscriptfile

Solution 3 - Bash

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.

Solution 4 - Bash

It also can be run by exporting the bash and sh of the gitbash C:\Program Files\git\bin\ in the Advance section of the environment variable of the Windows Server.

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.

Then,

Run the shell file as

bash shellscript.sh or sh shellscript.sh

Solution 5 - Bash

Simplest Way (Windows10)

./your_script.sh

But you have to enable script running on PowerShell See Here

Solution 6 - Bash

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionwsorensonView Question on Stackoverflow
Solution 1 - BashMarius TancrediView Answer on Stackoverflow
Solution 2 - BashTheBonsaiView Answer on Stackoverflow
Solution 3 - BashRynantView Answer on Stackoverflow
Solution 4 - BashGangaRam DewasiView Answer on Stackoverflow
Solution 5 - Bashghost21bladeView Answer on Stackoverflow
Solution 6 - BashNazarView Answer on Stackoverflow