Running Bash script with Ubuntu on Windows 10 using WSL

  • HOME
  • >
  • WINDOWS
  • >
  • Running Bash script with Ubuntu on Windows 10 using WSL
Last Updated: 

Windows 10 provide a neat integration with the Linux kernel which allows you to run any binary from your favorite Linux distribution directly from Windows. This feature is called Windows Subsystem for Linux (WSL) and it opened a whole new world of opportunities.

  1. Start an elevated PowerShell session using the shortcuts Win+X A
  2. Enable WSL:
    Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux. You must restart after enabling this feature.

👉 The first version of WSL come with some drawback and performance issues so you may want to use WSL2 instead. To do so, you will need to become a Windows insider as it requires a Windows 10 build 18917 or higher. You can check the well-documented steps on Microsoft Docs.

From there, you can either use the Windows Store (recommended) to install a Linux distribution available like Kali Linux or Ubuntu 20.04 LTS. If you choose this path, then once you installed the Ubuntu distro, you can directly launch Bash from a command prompt Win+r then bash.

👉 If you are new to Ubuntu 20.04 LTS, see the post What's New In Focal Fossa Ubuntu 20.04 LTS?.

Install from a WSL RootFS Image

To get a more recent distribution or a development one, you may need to install directly from a rootfs image. The example below download a Ubuntu 19.10. Make sure to replace <YourUserDir> with your actual Windows user directory.

  1. After rebooting, start a PowerShell session using the shortcuts Win+X i
  2. Download the Ubuntu 19.10 wsl rootfs tar file:
    Invoke-WebRequest https://cloud-images.ubuntu.com/eoan/current/eoan-server-cloudimg-amd64-wsl.rootfs.tar.gz -OutFile C:\Users\<YourUserDir>\Downloads\eoan-server-cloudimg-amd64-wsl.rootfs.tar.gz
  3. We will keep the manually installed distro in a WSL folder:
    mkdir C:\Users\<YourUserDir>\WSL
  4. Import the image to WSL:
    wsl --import Ubuntu-19.10 C:\Users\<YourUserDir>\WSL\Ubuntu-19.10 C:\Users\<YourUserDir>\Downloads\eoan-server-cloudimg-amd64-wsl.rootfs.tar.gz
  5. Check the installed distribution with:
    wsl --list.
  6. Start our newly installed distribution using: wsl -d Ubuntu-19.10
  7. You are now logged into your Linux shell. Check your bash version: bash --version
root@DESKTOP-XXXXXXX:~# bash --version
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2019 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>

This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

root@DESKTOP-XXXXXXX:~# echo $EPOCHSECONDS
1585712571

👉 To know more about the new bash version 5 $EPOCHSECONDS variable, check the post What's New in GNU Bash 5?

Related windows posts that you may like...
8 Tips to Get the Most of an Old Windows XP
8 command line tips to get the most value of your old Windows XP, Windows Server 2003, and others.
Removing ^M in imported Windows files
Learn how to quickly and easily handle files with newline and carriage return between linux, mac, and windows. This post will explain how to remove or replace the often annoying ^M character.
Delete old files by the last access date
Full example on how to delete files based on their creation date using a batch script on Windows XP. This post cover the basics of using scripts arguments in batch, parsing dates, and manipulating files.