在 Windows 中运行 Linux 命令的 4 种方法
Windows WSL

> 
> 想要使用 Linux 命令，但又不想离开 Windows ？以下是在 Windows 中运行 Linux bash 命令的几种方法。
> 
> 
> 


如果你正在课程中正在学习 shell 脚本，那么需要使用 Linux 命令来练习命令和脚本。


你的学校实验室可能安装了 Linux，但是你自己没有安装了 [Linux 的笔记本电脑](https://itsfoss.com/get-linux-laptops/)，而是像其他人一样的 Windows 计算机。你的作业需要运行 Linux 命令，你或许想知道如何在 Windows 上运行 Bash 命令和脚本。


你可以[在双启动模式下同时安装 Windows 和 Linux](https://itsfoss.com/guide-install-linux-mint-16-dual-boot-windows/)。此方法能让你在启动计算机时选择 Linux 或 Windows。但是，为了运行 Linux 命令而使用单独分区的麻烦可能不适合所有人。


你也可以[使用在线 Linux 终端](https://itsfoss.com/online-linux-terminals/)，但你的作业无法保存。


好消息是，有几种方法可以在 Windows 中运行 Linux 命令，就像其他常规应用一样。不是很酷吗？


### 在 Windows 中使用 Linux 命令


![](/data/attachment/album/201906/04/093809hlz2tblfzt7mbwwl.jpg)


作为一个热心的 Linux 用户和推广者，我希望看到越来越多的人使用“真正的” Linux，但我知道有时候，这不是优先考虑的问题。如果你只是想练习 Linux 来通过考试，可以使用这些方法之一在 Windows 上运行 Bash 命令。


#### 1、在 Windows 10 上使用 Linux Bash Shell


你是否知道可以在 Windows 10 中运行 Linux 发行版？ [Windows 的 Linux 子系统 （WSL）](https://itsfoss.com/bash-on-windows/) 能让你在 Windows 中运行 Linux。即将推出的 WSL 版本将在 Windows 内部使用真正 Linux 内核。


此 WSL 也称为 Bash on Windows，它作为一个常规的 Windows 应用运行，并提供了一个命令行模式的 Linux 发行版。不要害怕命令行模式，因为你的目的是运行 Linux 命令。这就是你所需要的。


![Ubuntu Linux inside Windows](/data/attachment/album/201906/04/093824cbqqnuxwxw2bujdv.jpg)


你可以在 Windows 应用商店中找到一些流行的 Linux 发行版，如 Ubuntu、Kali Linux、openSUSE 等。你只需像任何其他 Windows 应用一样下载和安装它。安装后，你可以运行所需的所有 Linux 命令。


![Linux distributions in Windows 10 Store](/data/attachment/album/201906/04/093827s4ftvv96cgfcxzx3.jpg)


请参考教程：[在 Windows 上安装 Linux bash shell](https://itsfoss.com/install-bash-on-windows/)。


#### 2、使用 Git Bash 在 Windows 上运行 Bash 命令


你可能知道 [Git](https://itsfoss.com/basic-git-commands-cheat-sheet/) 是什么。它是由 [Linux 创建者 Linus Torvalds](https://itsfoss.com/linus-torvalds-facts/) 开发的版本控制系统。


[Git for Windows](https://gitforwindows.org/) 是一组工具，能让你在命令行和图形界面中使用 Git。Git for Windows 中包含的工具之一是 Git Bash。


Git Bash 为 Git 命令行提供了仿真层。除了 Git 命令，Git Bash 还支持许多 Bash 程序，如 `ssh`、`scp`、`cat`、`find` 等。


![Git Bash](/data/attachment/album/201906/04/093827fdzpf94zpa4zff2f.png)


换句话说，你可以使用 Git Bash 运行许多常见的 Linux/Bash 命令。


你可以从其网站免费下载和安装 Git for Windows 工具来在 Windows 中安装 Git Bash。


* [下载 Git for Windows](https://gitforwindows.org/)


#### 3、使用 Cygwin 在 Windows 中使用 Linux 命令


如果要在 Windows 中运行 Linux 命令，那么 Cygwin 是一个推荐的工具。Cygwin 创建于 1995 年，旨在提供一个原生运行于 Windows 中的 POSIX 兼容环境。Cygwin 是由 Red Hat 员工和许多其他志愿者维护的自由开源软件。


二十年来，Windows 用户使用 Cygwin 来运行和练习 Linux/Bash 命令。十多年前，我甚至用 Cygwin 来学习 Linux 命令。


![Cygwin](/data/attachment/album/201906/04/093828rf30f7f1hccch9p7.jpg)


你可以从下面的官方网站下载 Cygwin。我还建议你参考这个 [Cygwin 备忘录](http://www.voxforge.org/home/docs/cygwin-cheat-sheet)来开始使用。


* [下载 Cygwin](https://www.cygwin.com/)


#### 4、在虚拟机中使用 Linux


另一种方法是使用虚拟化软件并在其中安装 Linux。这样，你可以在 Windows 中安装 Linux 发行版（带有图形界面）并像常规 Windows 应用一样运行它。


这种方法要求你的系统有大的内存，至少 4GB ，但如果你有超过 8GB 的内存那么更好。这里的好处是你可以真实地使用桌面 Linux。如果你喜欢这个界面，那么你可能会在以后决定[切换到 Linux](https://itsfoss.com/reasons-switch-linux-windows-xp/)。


![Ubuntu Running in Virtual Machine Inside Windows](/data/attachment/album/201906/04/093829uonfnkniktn0jttt.jpg)


有两种流行的工具可在 Windows 上创建虚拟机，它们是 Oracle VirtualBox 和 VMware Workstation Player。你可以使用两者中的任何一个。就个人而言，我更喜欢 VirtualBox。


你可以按照[本教程学习如何在 VirtualBox 中安装 Linux](https://itsfoss.com/install-linux-in-virtualbox/)。


### 总结


运行 Linux 命令的最佳方法是使用 Linux。当选择不安装 Linux 时，这些工具能让你在 Windows 上运行 Linux 命令。都试试看，看哪种适合你。




---


via: <https://itsfoss.com/run-linux-commands-in-windows/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

If you are learning Shell scripting probably as a part of your course curriculum, you need to use Linux commands to practice the commands and scripting.

Your school lab might have Linux installed but you don’t have a [Linux laptop](https://itsfoss.com/get-linux-laptops/) but a regular Windows computer like everyone else. Your homework needs to run Linux commands and you wonder how to run Bash commands and scripts on Windows.

You can [install Linux alongside Windows in dual boot mode](https://itsfoss.com/guide-install-linux-mint-16-dual-boot-windows/). This method allows you to choose either Linux or Windows when you start your computer. But taking all the trouble to mess with partitions for the sole purpose of running Linux commands may not be for everyone.

You can also [use Linux terminals online](https://itsfoss.com/online-linux-terminals/) but your work won’t be saved here.

The good news is that there are several ways you can run Linux commands inside Windows, like any regular application. Isn’t it cool?

## Using Linux commands inside Windows

![Run Linux Commands In Windows](https://itsfoss.com/content/images/wordpress/2019/05/run-linux-commands-in-windows-800x450.png)

As an ardent Linux user and promoter, I would like to see more and more people using ‘real’ Linux but I understand that at times, that’s not the priority. If you are just looking to practice Linux to pass your exams, you can use one method to run Bash commands on Windows.

### 1. Use Linux Bash Shell on Windows 10 with WSL

Did you know that you can run a Linux distribution inside Windows 10? The [Windows Subsystem for Linux (WSL)](https://itsfoss.com/bash-on-windows/) allows you to run Linux inside Windows. The upcoming version of WSL will be using the real Linux kernel inside Windows.

This WSL, also called Bash on Windows, gives you a Linux distribution in command-line mode running as a regular Windows application. Don’t be scared with the command line mode because your purpose is to run Linux commands. That’s all you need.

![Using Bash on Windows 10](https://itsfoss.com/content/images/wordpress/2016/08/install-ubuntu-windows-10-linux-subsystem-10-800x268.jpeg)

You can find some popular Linux distributions like Ubuntu, Kali Linux, openSUSE etc in Windows Store. You just have to download and install it like any other Windows application. Once installed, you can run all the Linux commands you want.

![Install Windows Subsyetm for Linux](https://itsfoss.com/content/images/wordpress/2016/08/install-ubuntu-windows-10-linux-subsystem-4-800x632.jpeg)

Please refer to this tutorial about [installing Linux bash shell on Windows](https://itsfoss.com/install-bash-on-windows/).

[How to Install Linux Bash Shell on Windows [Step-by-Step Guide]Step-by-step screenshot guide to show you how to install bash on Windows 11 and 10.](https://itsfoss.com/install-bash-on-windows/)![](https://itsfoss.com/content/images/2023/01/install-bash-in-windows-1.png)


![](https://itsfoss.com/content/images/2023/01/install-bash-in-windows-1.png)

### 2. Use Git Bash to run Bash commands on Windows

You probably know what [Git](https://itsfoss.com/basic-git-commands-cheat-sheet/) is. It’s a version control system developed by [Linux creator Linus Torvalds](https://itsfoss.com/linus-torvalds-facts/).

[Git for Windows](https://gitforwindows.org/) is a set of tools that allows you to use Git in both command line and graphical interfaces. One of the tools included in Git for Windows is Git Bash.

Git Bash application provides and emulation layer for Git command line. Apart from Git commands, Git Bash also supports many Bash utilities such as ssh, scp, cat, find etc.

![Git Bash](https://itsfoss.com/content/images/wordpress/2019/05/git-bash.png)

In other words, you can run many common Linux/Bash commands using the Git Bash application.

You can install Git Bash in Windows by downloading and installing the Git for Windows tool for free from its website.

### 3. Using Linux commands in Windows with Cygwin

If you want to run Linux commands in Windows, Cygwin is a recommended tool. Cygwin was created in 1995 to provide a POSIX-compatible environment that runs natively on Windows. Cygwin is a free and open source software maintained by Red Hat employees and many other volunteers.

For two decades, Windows users use Cygwin for running and practicing Linux/Bash commands. Even I used Cygwin to learn Linux commands more than a decade ago.

![Cygwin Shell](https://itsfoss.com/content/images/wordpress/2019/05/cygwin-shell.jpg)

[Image Credit](http://www.es.ele.tue.nl/education/5JJ55-65/mmips-lab/cygwin.php)

You can download Cygwin from its official website below. I also advise you to refer to this [Cygwin cheat sheet](http://www.voxforge.org/home/docs/cygwin-cheat-sheet) to get started with it.

### 4. Use Linux in a virtual machine

Another way is to use virtualization software and install Linux in it. This way, you install a Linux distribution (with a graphical interface) inside Windows and run it like a regular Windows application.

This method requires that your system has a good amount of RAM, at least 4 GB but better if you have over 8 GB of RAM. The good thing here is that you get the real feel of using a desktop Linux. If you like the interface, you may later decide to [switch to Linux](https://itsfoss.com/reasons-switch-linux-windows-xp/) completely.

![Ubuntu Running In Virtual Machine Inside Windows](https://itsfoss.com/content/images/wordpress/2019/05/ubuntu-running-in-virtual-machine-inside-windows-800x450.jpeg)

There are two popular tools for creating virtual machines on Windows, Oracle VirtualBox and VMware Workstation Player. You can use either of the two. Personally, I prefer VirtualBox.

You can follow [this tutorial to learn how to install Linux in VirtualBox](https://itsfoss.com/install-linux-in-virtualbox/).

[Install Linux Inside Windows Using VirtualBox [Step by Step Guide]Brief: Using Linux in a virtual machine gives you the option to try Linux within Windows. This step-by-step guide shows you how to install Linux inside Windows using VirtualBox. There are several ways to install Linux. You can clean everything from your system and install Linux. You can dual bo…](https://itsfoss.com/install-linux-in-virtualbox/)![](https://itsfoss.com/content/images/wordpress/2018/03/install-linux-inside-windows-featured.jpg)


![](https://itsfoss.com/content/images/wordpress/2018/03/install-linux-inside-windows-featured.jpg)

## Conclusion

The best way to run Linux commands is to use Linux. When installing Linux is not an option, these tools allow you to run Linux commands on Windows. Give them a try and see which method is best suited for you.