Running Bash script with Ubuntu on Windows 10 using WSL
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.
Enable WSL and Install A Recommended Distro
- Start an elevated PowerShell session using the shortcuts Win+X A
- 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.
- After rebooting, start a PowerShell session using the shortcuts Win+X i
- 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
- We will keep the manually installed distro in a WSL folder:
- 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
- Check the installed distribution with:
- Start our newly installed distribution using:
wsl -d Ubuntu-19.10
- You are now logged into your Linux shell. Check your 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
$EPOCHSECONDSvariable, check the post What's New in GNU Bash 5?