有没有办法在 Windows 上运行 Bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6413377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:29:42  来源:igfitidea点击:

Is there a way to run Bash scripts on Windows?

windowsbash

提问by Tower

I have bought and I use Windows 7 Ultimate, and I like to use it to develop applications. One of the down sides (as with every OS) is that I can not run Bash scripts. Is there a way to run Bash scripts on Windows by installing some software? It is ok if it does not work 100%, but as long as the most common functionality is available it should be great.

我购买并使用了 Windows 7 Ultimate,我喜欢用它来开发应用程序。缺点之一(与每个操作系统一样)是我无法运行 Bash 脚本。有没有办法通过安装一些软件在 Windows 上运行 Bash 脚本?如果它不能 100% 工作也没关系,但是只要最常用的功能可用,它就应该很棒。

采纳答案by Guss

Install Cygwin, which includes Bash among many other GNU and Unix utilities (without whom its unlikely that bash will be very useful anyway).

安装Cygwin,其中包括 Bash 以及许多其他 GNU 和 Unix 实用程序(没有它,bash 无论如何都不太可能非常有用)。

Another option is MinGW's MSYSwhich includes bash and a smaller set of the more important utilities such as awk. Personally I would have preferred Cygwin because it includes such heavy lifting tools as Perl and Python which I find I cannot live without, while MSYS skimps on these and assumes you are going to install them yourself.

另一种选择是MinGW 的 MSYS,它包括 bash 和一组较小的更重要的实用程序,例如 awk。就我个人而言,我更喜欢 Cygwin,因为它包含诸如 Perl 和 Python 之类的繁重工具,我发现我离不开这些工具,而 MSYS 忽略了这些工具并假设您要自己安装它们。

Updated: If anyone is interested in this answer and is running MS-Windows 10, please note that MS-Windows 10 has a "Windows Subsystem For Linux" feature which - once enabled - allows you to install a user-mode image of Ubuntu and then run Bash on that. This provides 100% compatibility with Ubuntu for debugging and running Bash scripts, but this setup is completely standalone from Windows and you cannot use Bash scripts to interact with Windows features (such as processes and APIs) except for limited access to files through the DrvFSfeature.

更新:如果有人对此答案感兴趣并且正在运行 MS-Windows 10,请注意 MS-Windows 10 具有“适用于 Linux 的 Windows 子系统”功能 - 一旦启用 - 允许您安装 Ubuntu 和然后在其上运行 Bash。这为调试和运行 Bash 脚本提供了与 Ubuntu 100% 的兼容性,但此设置完全独立于 Windows,除了通过DrvFS功能对文件的有限访问之外,您不能使用 Bash 脚本与 Windows 功能(例如进程和 API)进行交互.

回答by Spencer Rathbun

If your looking for something a little more native, you can use getGnuWin32to install all of the unix command line tools that have been ported. That plus winBashgives you mostof a working unix environment. Add console2for a better terminal emulator and you almost can't tell your on windows!

如果您正在寻找更原生的东西,您可以使用getGnuWin32来安装所有已移植的 unix 命令行工具。再加上winBash为您提供了大部分工作的 unix 环境。添加console2以获得更好的终端模拟器,您几乎无法在 Windows 上告诉您!

Cygwin is a better toolkit overall, but I have found myself running into suprise problems because of the divide between it and windows. None of these solutions are as good as a native linux system though.

Cygwin 总体上是一个更好的工具包,但我发现自己遇到了令人惊讶的问题,因为它与 Windows 之间存在分歧。不过,这些解决方案都没有本地 linux 系统那么好。

You may want to look into using virtualboxto create a linux VM with your distro of choice. Set it up to share a folder with the host os, and you can use a true linux development environment, and share with windows. Just watch out for those EOL markers, they get ya every time.

您可能需要考虑使用virtualbox来创建您选择的发行版的 linux VM。设置为与主机os共享文件夹,即可使用真正的linux开发环境,与windows共享。请注意那些 EOL 标记,他们每次都会收到。

回答by Garvit Jain

Best Option I could find is Git WindowsJust install it and then right click on and click "Git Bash Here" this will open a bash windowenter image description here

我能找到的最佳选择是Git Windows只需安装它,然后右键单击并单击“Git Bash Here”,这将打开一个 bash 窗口在此处输入图片说明

This will open a bash window like this: enter image description here

这将打开一个 bash 窗口,如下所示: 在此处输入图片说明

and the linux commands work...

和 linux 命令工作......

I've tried 'sh' , 'vi' , 'ssh' , 'curl' ,etc... commands

我试过 'sh' , 'vi' , 'ssh' , 'curl' 等...命令

回答by Paul0515

Best option? Windows 10. Native Bash support!

最佳选择?Windows 10。本机 Bash 支持!

回答by Michael Shopsin

You can always install Cygwinto run a Unix shell under Windows. I used Cygwin extensively with Window XP.

您始终可以安装Cygwin以在 Windows 下运行 Unix shell。我在 Window XP 中广泛使用了 Cygwin。

回答by Simon Featherstone

After installing git-extentions (https://code.google.com/p/gitextensions/) you can run .sh file from the command prompt. (No ./script.sh required, just run it like a bat/cmd file) Or you can run them in a "full" bash environment by using the MinGW Git bash shell.

安装 git-extensions ( https://code.google.com/p/gitextensions/) 后,您可以从命令提示符运行 .sh 文件。(不需要 ./script.sh,只需像 bat/cmd 文件一样运行它)或者您可以使用 MinGW Git bash shell 在“完整”的 bash 环境中运行它们。

I am not a great fan of Cygwin (yes I am sure it's really powerful), so running bash scripts on windows without having to install it perfect for me.

我不是 Cygwin 的忠实粉丝(是的,我确信它真的很强大),所以在 Windows 上运行 bash 脚本而无需安装它对我来说是完美的。

回答by rubynorails

In order to run natively, you will likely need to use Cygwin (which I cannot live without when using Windows). So right off the bat, +1 for Cygwin. Anything else would be uncivilized.

为了在本机运行,您可能需要使用 Cygwin(我在使用 Windows 时离不开它)。所以马上,Cygwin +1。其他任何事情都是不文明的。

HOWEVER, that being said, I have recently begun using a combination of utilities to easily PORT Bash scripts to Windows so that my anti-Linux coworkers can easily run complex tasks that are better handled by GNU utilities.

然而,话虽如此,我最近开始使用实用程序的组合来轻松地将 Bash 脚本移植到 Windows,以便我的反 Linux 同事可以轻松运行复杂的任务,这些任务可以由 GNU 实用程序更好地处理。

I can usually port a Bash script to Batch in a very short time by opening the original script in one pane and writing a Batch file in the other pane. The tools that I use are as follows:

我通常可以通过在一个窗格中打开原始脚本并在另一个窗格中写入 Batch 文件,在很短的时间内将 Bash 脚本移植到 Batch。我使用的工具如下:

I prefer UnxUtils to GnuWin32 because of the fact that [someone please correct me if I'm wrong] GnuWin utils normally have to be installed, whereas UnxUtils are standalone binaries that just work out-of-the-box.

我更喜欢 UnxUtils 而不是 GnuWin32,因为[如果我错了,请纠正我] GnuWin utils 通常必须安装,而 UnxUtils 是独立的二进制文件,可以开箱即用。

However, the CoreUtils do not include some familiar *NIX utilities such as cURL, which is also available for Windows (curl.haxx.se/download.html).

但是,CoreUtils 不包括一些熟悉的 *NIX 实用程序,例如 cURL,它也可用于 Windows (curl.haxx.se/download.html)。

I create a folder for the projects, and always SET PATH=. in the .bat file so that no other commands other than the basic CMD shell commands are referenced (as well as the particular UnxUtils required in the project folder for the Batch script to function as expected).

我为项目创建了一个文件夹,并始终设置 PATH=。在 .bat 文件中,以便除了基本的 CMD shell 命令之外没有其他命令被引用(以及项目文件夹中的特定 UnxUtils 需要批处理脚本按预期运行)。

Then I copy the needed CoreUtils .exe files into the project folder and reference them in the .bat file such as ".\curl.exe -s google.com", etc.

然后我将所需的 CoreUtils .exe 文件复制到项目文件夹中,并在 .bat 文件中引用它们,例如“.\curl.exe -s google.com”等。

The Bat2Exe program is where the magic happens. Once your Batch file is complete and has been tested successfully, launch Bat2Exe.exe, and specify the path to the project folder. Bat2Exe will then create a Windows binary containing all of the files in that specific folder, and will use the first .bat that it comes across to use as the main executable. You can even include a .ico file to use as the icon for the final .exe file that is generated.

Bat2Exe 程序是神奇发生的地方。批处理文件完成并成功测试后,启动 Bat2Exe.exe,并指定项目文件夹的路径。Bat2Exe 然后将创建一个包含该特定文件夹中所有文件的 Windows 二进制文件,并将使用它遇到的第一个 .bat 作为主可执行文件。您甚至可以包含一个 .ico 文件,用作生成的最终 .exe 文件的图标。

I have tried a few of these type of programs, and many of the generated binaries get flagged as malware, but the Bat2Exe version that I referenced works perfectly and the generated .exe files scan completely clean.

我尝试了一些此类程序,许多生成的二进制文件被标记为恶意软件,但我引用的 Bat2Exe 版本运行良好,生成的 .exe 文件扫描完全干净。

The resulting executable can be run interactively by double-clicking, or run from the command line with parameters, etc., just like a regular Batch file, except you will be able to utilize the functionality of many of the tools that you will normally use in Bash.

生成的可执行文件可以通过双击以交互方式运行,或从带有参数的命令行运行等,就像常规的批处理文件一样,除了您将能够利用您通常会使用的许多工具的功能在巴什。

I realize this is getting quite long, but if I may digress a bit, I have also written a Batch script that I call PortaBashy that my coworkers can launch from a network share that contains a portable Cygwin installation. It then sets the %PATH% variable to the normal *NIX format (/usr/bin:/usr/sbin:/bin:/sbin), etc. and can either launch into the Bash shell itself or launch the more-powerful and pretty MinTTY terminal emulator.

我意识到这已经很长了,但如果我可以离题一点,我还编写了一个批处理脚本,我称之为 PortaBashy,我的同事可以从包含便携式 Cygwin 安装的网络共享启动它。然后它将 %PATH% 变量设置为正常的 *NIX 格式(/usr/bin:/usr/sbin:/bin:/sbin)等,并且可以启动到 Bash shell 本身或启动更强大的漂亮的 MinTTY 终端模拟器。

There are always numerous ways to accomplish what you are trying to set out to do; it's just a matter of combining the right tools for the job, and many times it boils down to personal preference.

总是有很多方法可以完成你想要做的事情;这只是为工作组合合适的工具的问题,很多时候归结为个人喜好。

回答by vines

There's one more theoretical possibility to do it: professional versions of Windows have built-in POSIX support, so bash could have been compiled for Windows natively.

还有一种理论上的可能性:Windows 的专业版本具有内置的 POSIX 支持,因此 bash 可以为 Windows 本地编译。

Pity, but I still haven't found a compiled one myself...

可惜,我自己还没找到编译好的...