从 Windows PowerShell 运行 bash 脚本

在 cygwin 中,我可以只做./script.sh args,但是这会在 PowerShell 的记事本中打开脚本文件。

我需要做什么来执行它?

301327 次浏览

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

sh myscriptfile

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.

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

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

Simplest Way (Windows10)

./your_script.sh

But you have to enable script running on PowerShell See Here

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