How can I use a Bash-like shell on Windows?

You can now install the Windows Subsystem for Linux which allows you to run native user-mode Linux shell and tools on Windows. This allows you to run Bash shell directly on Windows and is based on Ubuntu.

This was introduced in Windows 10 with the Anniversary Update (2016) and is not enabled by default. To enable the new feature on a Windows 10 Anniversary Update machine, do the following:

  1. Navigate to Settings -> Update & Security -> For Developers and select "Developer mode"
  2. Control Panel -> Programs and Features and click "Turn Windows Features on or off" on the top left pane
  3. In the dialog box, check "Windows Subsystem for Linux" and click Ok
  4. Reboot machine
  5. Search for "bash" and click on it, it should open a command prompt and ask you if you want to install "Ubuntu on Windows", continue with "y"
  6. After installation it will ask to create a UNIX username and password
  7. You are now ready to use the bash shell

A bit of history, back in 2015 project Astoria was started to bring Android apps to Windows and they built a Linux subsystem called Windows Subsystem for Linux (WSL) for the purpose. Eventually project Astoria was dropped as it undermined their UWP (Universal Windows Platform) strategy, but some parts were retained and form the basis of bash for Windows. Over the course, Microsoft collaborated with Canonical (creators of Ubuntu Linux) to provide them with a native image with all supported tools/binaries.

More information available here,
https://blogs.msdn.microsoft.com/commandline/2016/04/06/bash-on-ubuntu-on-windows-download-now-3/
https://blogs.windows.com/buildingapps/2016/03/30/run-bash-on-ubuntu-on-windows/
https://sec.ch9.ms/sessions/build/2016/P488.mp4 (video)


You could install Cygwin or MSYS/MinGW, or you could install a full Linux distribution in a virtual machine (for example, using VirtualBox).


Here's a list of the options available as of 2018-05 with a plenty of updates regarding WSL as of 2020-01 along with a bit of detail regarding each one.

Options that stand out

Windows Subsystem for Linux(WSL)

WSL is becoming more and more mature (Microsoft dropped the beta warning long ago) and is very easy to install so in most cases it's a very good (if not the best) option. It solves the problem using a different method than most other options. You run the exact same binary code that you would run on a Linux system. Until 2020-01 WSL intercepts kernel calls and translates them to windows kernel calls (think the opposite of Wine). This will change since current beta of WSL runs a real linux kernel in a lightweight VM.

Anyway, with WSL you do get a nice shell with almost all CLI goodies you'd expect to find on a stock Ubuntu installation and the option to apt install almost everything available from the repositories. Microsoft officially states that "WSL does not aim to support GUI desktops or applications" however there are unofficial instructions on how to do it. It seems that as things stand at the moment (2020-01) the main drawbacks of WSL are the following:

  • Rendering the screen and disk access are very slow
  • You are not supposed to edit WSL(Linux) files from Windows
  • It only runs on 64bit Windows.

Here's an old but nice comparison with cygwin (note that Rich Turner is a member of the WSL team and he has posted a lot of useful info regarding WSL)

Cygwin and Babun

Cygwin provides the following:

  1. a bash shell and the Mintty terminal,
  2. an extensive range of common Linux utilities compiled to run on Windows
  3. a comprehensive compatibility layer that enables Linux programs to run on Windows.

Cygwin aims to make porting Linux applications on Windows easy. If some C program was designed for Unix and expects to use features such as pipes, Unix-style file and directory access e.t.c. then you can compile it for Cygwin and Cygwin will act as a compatibility layer between the code and the foreign OS (Windows). This allows the same C program to work on both Linux and Windows with little or no modification. A huge list of Linux programs have been thus complied and can be run when you install Cygwin. However you, the user of these programs, will probably have to be familiar with Unix. For example you may need to pass arguments like /cygdrive/c/autoexec.bat instead of c:\autoexec.bat and be ready to deal with output with Unix line termination (\n) instead of Windows line termination (\r\n).

The collection of all Cygwin programs comprise a repository and although Cygwin doesn't have a traditional Linux package manager you can add, remove and update programs from this repository at any time with a very counter-intuitive way (you run Cygwin's setup.exe at any time after the installation has completed)

Babun is Cygwin with a full-featured package manager (pact), oh-my-zsh for its shell, git, automatic updates and a plug-in oriented architecture. Although the default shell is zsh you can easily switch to bash. It seems like a nice upgrade from vanilla Cygwin if you don't mind the extra weight.

Mingw-w64

Mingw-w64 provides the following:

  1. a bash shell,
  2. a fair amount of Linux utilities,
  3. a SW development toolchain to build apps that will run without a Linux compatibility layer (or with a minimal one).

MinGW is primarily a software distribution and a building platform for Windows. In particular it's a Windows port of the GNU compiler tools, such as GCC, make, bash, and so on. It includes a fair amount of GNU tools and a minimal Unix compatibility layer.

The main difference between Mingw-w64 and Cygwin is on priorities: Cygwin aims to provide a complete POSIX compatibility layer comprising a full implementation of all major Unix system calls and libraries; performance is secondary to compatibility. MinGW aims for performance so it will not provide certain POSIX APIs which cannot be implemented easily and/or fast enough under Windows. Also MinGW's main target are S/W developers. Nevertheless casual users may also enjoy its shell and the general purpose included utilities.

MSYS2 and Git for Windows

MSYS2 provides the following:

  1. a bash shell and the Mintty terminal,
  2. a fair amount of Linux utilities,
  3. a full featured command line package manager to install OS programs
  4. parts from both Cygwin and MinGW to support building applications either with or without a comprehensive Linux compatibility layer.
  5. a software repository and a package manager (pacman) which makes it easy to install, use, build and port software on Windows.

MSYS2 is also primarily a software distribution and a building platform for Windows like MinGW but it combines both the way of Cygwin and the way of MinGW. You can use MSYS2 to either create "MinGW programs" or "MSYS2 programs". The later link to the compatibility layer of MSYS2 (msys-2.dll) which itself closely follows Cygwin's compatibility layer development with a few additions/modifications. The repositories of the 2 projects are completely unrelated however. So although both MSYS2 and Cygwin provide grep.exe they may have a completely different version.

Git for Windows is either just MSYS2 with git installed or at least heavily based on MSYS2. It provides the same shell (bash), terminal (Mintty) and package manager (pacman). It appears to be the most popular way to get the dominant Git running on Windows and along with it you get a nice Unix environment.

Common ground

Except from WSL most of the above tools share a lot of common technology (libraries, executables, concepts). For example the bash shell that comes with MinGW depends on msys-2.dll which itself is a fork of cygwin.dll. So yes there's plenty of room for confusion :-)

More options

Cmder provides just a nice terminal and a bash-like shell for Windows. It's main component is Conemu (the terminal). On top of that it adds Clink which provides Powerful Bash-style command line editing, a custom prompt layout and the Monokai color scheme. It is highly (or maybe fully) compatible with native windows console programs.

Scoop provides a command line package manager for many well known cross-platform programs including many GNU tools. It downloads pre-compiled packages. It does not provide neither a shell nor terminal but instead runs under windows' cmd.exe (with all its limitations but also with its full compatibility with native windows console programs). It also includes no compiler suite (but of course compilers and development tools are typical packages you can install with scoop). A lot of the programs that Scoop installs either come directly from the MinGW/MSYS project, or were built using their tools.

Even more options

The solutions bellow don't seem active and I never tested them but they seem to do the job for others:

Gow (Gnu On Windows) is the lightweight alternative to Cygwin without a shell. It uses a convenient Windows installer that installs about 130 extremely useful open source Linux applications compiled as native win32 binaries and available through windows' cmd.exe. It is designed to be as small as possible (about 10 MB).

UnxUtils & GnuWin32: UnxUtils is a collection of ports of common GNU Unix-like utilities to native Win32, with executables only depending on the Microsoft C-runtime msvcrt.dll. You must download the main part and a few updates. GnuWin32 has later versions than UnxUtils, but requires supporting files (e.g. DLLs)

MSYS & MinGW seem to have been obsoleted by MSYS2 & MinGW-W64 so I never looked at them.

Some common issues on Cygwin & MSYS2

Regarding the terminal

Mintty is the terminal used in Cygwin, MSYS2 and their derivatives. One should be aware that if you're running windows native console programs it is not a painless replacement for the Windows Command Prompt. While programs with simple text output usually work fine, interactive and full screen ones often have problems. Read more on Mintty's home page and also read the entry "Some native console programs don't work when run from Git Bash" on Git for Windows FAQ. That entry has this recommendations to make when you face this kind of problems:

There are several methods for working around these problems:

  • Run programs that have problems using the winpty utility. This allows you to keep using the nicer mintty terminal, but can become unwieldy if you need the workaround for many programs.
  • [use cmd.exe] and configure it for "Quick Edit", reasonable size and scroll-back and suitable unicode font. You'll still have to live with the other quirks of [cmd.exe].
  • Install and use Conemu.

Other issues

The two most important things to keep in mind are these:

  1. there's a trade-of between power and hard to debug issues. The more powerful the solution the more stuff can go wrong even in ways that will initially seem unrelated to your Linux-layer.

  2. if you don't have plenty of Linux experience don't use the more powerful solutions like Cygwin/MSYS2/MinGW

UnxUtils and GnuWin32 have the least power but also the least potential of causing you headaches. Cygwin and similar will top the bar in both power and headaches. Cmder is somewhere at the middle. So when you need the most powerful solutions take a good sleep and be fully concentrated. Avoid using powerful solutions while testing something new and possibly unstable. Also don't forget that medium complexity solutions like cmder are not spotless.

The issue of Linux commands shadowing windows ones is a common cause of hard to debug trouble in solutions like Cygwin/MSYS2/MinGW. Here's an example: I had a .bat file that was using the timeout command. When run under Cygwin it would fail because timeout is also a Linux command but with different syntax. After I've spotted the issue I found out I could add a PATH=... at the top of the .bat file to make sure the windows command had priority. But then I got an even more cryptic "Input redirection is not supported" error and settled for a workaround before finding the reason.

Here's an example of an issue with cmder. I was running the Windows version of unison under Cmder and under some special conditions it was hanging with absolutely no error message. The same command was always running perfectly under cmd.exe. The funny thing is that it was also running perfectly under Cygwin (in fact if you have Unicode filenames a Cygwin terminal is better than cmd.exe because cmd.exe often displays Unicode text malformed).

About this guide

During 2017-05 I got sick of not understanding the differences of the above tools although I was using some of them since long ago (mainly CygWin and cmder but never for development purposes). So I spent a few hours to grok the landscape. This guide is the result of the notes I kept. I'm not an expert on the field but I've tried hard to find all relevant information, read it with a critical eye and present the most important parts clearly. Please comment if somethings seems wrong and I'll do my best to correct it.

Finally an apology for some terminology issues: In this text I sometimes use the term Linux in place of UNIX or POSIX. I know they are not the same but it's very difficult to differentiate between them when approaching a topic from such a long distance. Even when it's not difficult it takes time that I don't have ;-)