Discover the power of Bash on Windows
- 13 March, 2017 21:00
Microsoft Windows may be the dominant player on the desktop, but the rapidly increasing open source software market—especially for admin and dev tools—clearly favors Linux. Not to mention the mobile market, where Android uses Linux variants. If you’re a developer on Windows, the drumbeat to get hip to Linux capabilities keeps getting louder.
Over the years, Microsoft has introduced various workarounds for using Linux capabilities on Windows, such as PowerShell with SSH and Cygwin and MSYS. Running Linux inside a virtual machine is another option. But VMs consume a significant amount of resources and don’t provide a first-class Linux experience, as you can’t edit local files or get full access to local drives, for example.
As the IT world is turning toward Linux for many projects, Microsoft has come up with a new offering to tap into this growing market. Bash on Windows is the answer. Here we guide you through installing Bash on Windows and give you a taste of what you can do—and why you would do it—in the Linux command line.
An overview of Bash on Windows
Bash on Windows is a new feature added to Windows 10. Microsoft has teamed up with Canonical, aka the creators of Ubuntu Linux, to build this new infrastructure within Windows called the Windows Subsystem for Linux (WSL). It allows developers to access a complete set of Ubuntu CLI and utilities. With a native Linux experience, developers can run Linux commands on Windows, including access to local files and drives. As Linux is natively integrated into Windows, developers get the flexibility to work on the same file in Linux and Windows. Simply put, Bash on Windows brings Ubuntu userland to Windows minus the Linux kernel.
Bash vs. PowerShell
Microsoft already has a command shell in PowerShell. So how does Bash on Windows differ? PowerShell is Microsoft’s configuration management framework for automating tasks. It’s used to manage Windows with its API-oriented architecture. Bash, on the other hand, mostly relies on text files for automation and development. The two differ in both focus and design. For example, when you list files in a directory using the
ls command, PowerShell displays the output as file objects, whereas Bash on Windows displays the output as a set of strings. Thankfully, for Windows admins, you can work with both solutions side by side and get the best of both worlds.
One word of caution: PowerShell has aliases that allow you to think you’re running traditional Bash commands when, in fact, you’re running PowerShell cmdlets. This may trip up some people. For instance,
ls is an alias for the
Get-ChildItem command. Similarly,
pwd is an alias for
cd is an alias for
Set-Location. For a list of all aliases in PowerShell, use the
Bash on Windows provides multiple benefits to open source developers. By bringing native Linux capabilities to Windows, Bash on Windows eliminates the need to run dual booting with Ubuntu for accessing Linux capabilities. You don’t need to go for Mac OS X, run a virtual machine, or create workarounds using Cygwin. It gives the required toolset to write and build code for your scenarios and platforms. By accessing the Windows file system from Bash, you can work on the same files using Windows or the Linux CLI.
How did Microsoft port Bash to Windows?
At the April 2016 Build Conference, Microsoft announced the Windows Subsystem for Linux (WSL) to the surprise of many. Born of a partnership with Canonical, Bash on Windows first shipped with Windows 10 Anniversary Update. It comes in two parts: the core subsystem and a package. The core subsystem is already a part of Windows 10 Insider Builds and offers the Linux API on Windows, which means you can natively load Linux libraries and executables. Canonical provides the software package as an option. This software package offers Bash and CLI tools required for the Linux environment.
To run Bash on Windows, your system must run the x64 Windows 10 Anniversary Update Build 14393 or later. You discover the build by typing
winver in the command box.
If the build version is less than 14393, you will not be able to install Bash.
To enable Bash on a supported build, you should first turn on Developer Mode. To do so, head over to Settings, click on For Developers and select the Developer Mode radio button. Once this is done, you’ll be prompted to confirm.
Once this has been done, the Windows Subsystem for Linux feature must be enabled. The easiest way to do this is through a PowerShell cmdlet:
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux
To open Bash on Windows, open up the command prompt, type
bash, and accept the license agreement. When Bash installs, it performs a few changes to your system:
- An Ubuntu user-mode image is downloaded.
- A hidden folder located at
- A shortcut is placed on the desktop.
To run Bash, you can now either go to the command prompt or use the desktop shortcut icon.
After the successful installation of Bash, the system will prompt you to create a Unix username and password. This username and password is for Bash and doesn’t relate in any way to your Windows environment.
Getting started with commands
Once in Bash, you have a few available commands to manage WSL and the Ubuntu image.
lxrun: For managing the WSL instance
lxrun/install: To start the download and install procedure
lxrun/uninstall: To uninstall the Ubuntu image
lxrun/update: To update the WSL package index
lxrun/setdefaultuser<username>: To set the default Bash on Ubuntu user
Bash on Windows also, of course, allows you to run many “traditional” Bash commands. For example:
grep: To find lines that match a pattern
sed: To substitute a string
echo: To output the value to the screen
var=2: To create a variable for
=!=: To compare small snippets of texts
For navigation, you can also use the Windows DOS command
cd to navigate the folder structure.
cd temp: Changes the working directory to the folder named temp
cd\: Takes you to the root directory. As Ubuntu doesn’t have drive letters like Windows, the root directory would be the top-level directory.
cd..: Takes the prompt one level upward (that is, to the parent directory)
cd~: Takes you to the home directory
cd in PowerShell is slightly different in Bash on Ubuntu. In the WSL environment, your Windows drives are stored in the
/mnt folder, and the name of the drive is used as the subfolder. When you use the
cd command, you should change the path accordingly.
To check the path and contents of a directory, the following are a couple of examples:
pwd: Prints the path or the directory you are into the screen
ls: Displays the files in a directory
If you want to understand the functionality of and the parameters associated with each command, the
man command can be used.
man <command> and it will display the synopsis of what the command is for and the associated parameters. This is similar to the PowerShell
One of the advantages of Bash is that you are working with plain text. This means you don’t have to mess with the registry or program settings. Whether you want to change the boot sequence or the web server configuration, simply edit the corresponding text file. To efficiently perform editing tasks, you need a good text editor. Bash offers powerful editors to make your job easy. A few good examples of text editors available within Bash are
Package management in Bash
Since you’re essentially running Linux, you also now have package management commands available in the form of
apt-get. A few examples:
sudo apt-get update: Refreshes the list of repositories to pull from
sudo apt-get upgrade: Upgrades all the software to the latest version
apt-cache search app_name: Searches the repository for a particular app
sudo apt-get install apt-name: Downloads and installs the particular app
sudo is prepended to all commands to allow you to run a command under a different user, the superuser (or admin) as default. It is similar to the “run as administrator” technique in Windows.
To download files via HTTP from a server or a URL, you now can use the
curl command. This command is similar to PowerShell’s
Similarly, you can connect to another remote Linux computer and run commands on it. Since we’re now in Linux, we finally have a true SSH client to work with. Rather than using Cygwin or the current beta of the OpenSSH implementation for Windows, we now can run SSH natively via the
ssh username @abc.com
While we’re on the topic of OpenSSH, we also now have the native ability to copy files securely over SCP using the built-in
scp command as well:
scp localfile firstname.lastname@example.org:remotedirectory/remotefile
With Bash on Windows, you now have a lot of other features at your fingertips:
- Tools such as Git, Python, and Ruby directly on Windows
- Command-line editors such as emacs and vi
- Accessing the Windows file system from the Bash environment
- Linux user support
- Symlink support
- Storage mounting via
What are common use cases for Bash on Windows?
Currently, many developers use Windows and Linux tools, which means they have to perform dual-booting or run Linux inside a virtual machine to get the CLI capabilities. When they can run these tools within Windows, then Windows becomes their primary desktop. In that case, they don’t have to port Linux apps and services to Windows systems. While some people have been able to run Linux GUI apps inside Windows, many people get a handy Linux CLI to perform simple tasks without purchasing a dedicated Linux machine from Azure or move to a Mac device for simple purposes. As scripting is easy on Bash, you can automate development tasks such as backing up websites in Apache.
For instance, many developers use GitHub for version control tasks. To access GitHub on Windows, you have to install GitHub for Windows, then use commit and push commands to commit changes. Alternately, you have to download the Git utility, which is a tedious process. With Bash on Windows, your job becomes simple:
apt-get install git
Also, under Bash, you don’t need any third-party tools to work with traditional Linux file types like GZIPed tarballs (tar.gz files).
What are the drawbacks of using Bash on Windows?
Bash on Windows is still in beta and contains some rough edges. Microsoft says Bash on Windows might not perfectly execute all of your scripts at this time. However, Microsoft is keenly monitoring the performance and feedback, and it’s proactively making changes to this solution.
Secondly, Bash on Windows was brought about for the development community. It was not designed for managing Windows environments. Though you can do things like run server daemons in WSL, it doesn’t offer the full capabilities of a full Linux virtual machine. If you intend to run server processes under production workloads on Ubuntu, you should look at other alternatives for running a full Linux operating system.
Finally, Bash on Windows brings the Linux capabilities to Windows. However, Linux tools won’t be able to interact with Windows tools and applications. This means no cross-platform capabilities due to their inherent architecture differences. For example, you’ll never be able to invoke Win32-native applications from within Bash.
Bash on Windows is still in its nascent stage. Though there are some restrictions to this solution, Microsoft is keenly working on adding more capabilities to erase these differences and make Windows the No. 1 platform for every type of development project. Keep an eye on this project from Microsoft. With Microsoft’s new stance in the open source world, it is sure to make Bash a first-class citizen in Windows as time progresses.