Using Bash on Windows with WSL

Su Suriya Ravichandran Updated 06 Dec 2026
7 min read ·Lesson 9 of 205

Windows does not include Bash by default, because its own shells, Command Prompt and PowerShell, use different languages. The Windows Subsystem for Linux (WSL) closes that gap. It lets you run a real Linux distribution, with a real Bash, directly on Windows without a separate virtual machine window or a dual-boot setup. This lesson explains what WSL is, how to install it, how to start Bash, how Windows and Linux files interact, and what to watch for when scripts cross between the two worlds.

Understanding the Concept

WSL is a Windows feature that runs Linux programs. Current installations use WSL 2, which runs a real Linux kernel inside a lightweight virtual machine that Windows manages for you. From your point of view it behaves like an installed program:

  • You open a terminal and get a Linux shell.
  • The Linux environment has its own file system, package manager, and users.
  • Windows and Linux can reach each other's files and can run each other's programs.

Because it is a real Linux distribution, such as Ubuntu or Debian, the Bash you get is the same Bash described in the Linux lessons, and the commands from the earlier lessons work unchanged.

Requirements

WSL needs a supported version of Windows 10 or Windows 11 with virtualization enabled in the computer's firmware settings. Installation requires administrator rights. The steps below are the standard ones from Microsoft's documentation, so check the official WSL page if your Windows version is older than the current releases.

Installing WSL

Open PowerShell or Windows Terminal as an administrator and run:

wsl --install

This single command enables the required Windows features and installs a default Linux distribution, which is Ubuntu in current releases. Restart the computer when it asks you to. After the restart, the distribution finishes setting itself up and asks you to choose a Linux user name and password. These belong to the Linux environment only and are separate from your Windows account.

To see which distributions are available for installation:

wsl --list --online

To install a specific one, pass its name:

wsl --install -d Debian

The -d option selects the distribution by name, using a name from the online list.

Starting Bash in WSL

After installation, the distribution appears in the Start menu under its own name, such as Ubuntu. Opening it starts a terminal running Bash. You can also start it from PowerShell or Windows Terminal:

wsl

This opens your default distribution and starts its default shell. Because Ubuntu and Debian use Bash as the default shell, you land at a Bash prompt. Confirm it with commands you already know:

echo "$BASH_VERSION"
cat /etc/os-release | head -2

Sample output:

5.2.21(1)-release
PRETTY_NAME="Ubuntu 24.04.1 LTS"

The exact numbers depend on the distribution release you installed. Type exit to leave the Linux shell and return to PowerShell.

You can also run a single Bash command from Windows without opening an interactive session:

wsl ls -l /etc
wsl bash -c "echo hello from Bash"

Everything after wsl is passed to the Linux environment. The second form runs bash -c so that Bash itself interprets the quoted string.

Managing Distributions

Several commands control installed distributions from the Windows side:

CommandPurpose
wsl --list --verboseShow installed distributions, their state, and WSL version
wsl --set-default <name>Choose which distribution wsl opens by default
wsl --updateUpdate the WSL components
wsl --shutdownStop all running distributions and the WSL virtual machine
wsl --terminate <name>Stop one distribution

For example, wsl --list --verbose prints a table with the name, whether it is running or stopped, and the version number, where 2 means WSL 2.

Files in WSL

The Linux File System

Your Linux home directory lives inside the distribution's own file system. Inside Bash, it is /home/<your-user>, and ~ is shorthand for it. Work you do with Linux tools should normally stay here, because file access inside the Linux file system is fastest and keeps Linux permissions intact.

Reaching Windows Drives

WSL mounts your Windows drives under /mnt. The C: drive appears as /mnt/c:

ls /mnt/c/Users

The command lists the user folders on the Windows C: drive. You can read and change Windows files from Bash this way. Access across this boundary is slower than inside the Linux file system, so avoid keeping large projects with thousands of small files, such as dependency folders, under /mnt/c when you can keep them in your Linux home directory instead.

Reaching Linux Files from Windows

Windows can open Linux files through a network-style path that starts with 0wsl$ or 1wsl.localhost, followed by the distribution name. From inside Bash you can open the current directory in Windows File Explorer with:

explorer.exe .

Because WSL can run Windows programs, explorer.exe starts the Windows file manager at the Linux directory shown by ..

Converting Paths

Windows and Linux write paths differently. The wslpath utility converts between them:

wslpath "C:\Users\Asha\Documents"

Output:

/mnt/c/Users/Asha/Documents

In the other direction, wslpath -w /home/asha prints the Windows form of the Linux path. Inside Bash scripts, quote Windows-style paths and double the backslashes or use single quotes so Bash does not treat the backslash as an escape character.

Line Endings: A Common Problem

Windows text files traditionally end each line with two characters, a carriage return and a line feed (CRLF). Linux uses only a line feed (LF). A Bash script saved with Windows line endings fails in an unexpected way, because the invisible carriage return becomes part of every command.

A script containing a hidden carriage return shows the problem when displayed with cat -A, which marks the carriage return as ^M:

hello^M$

When you run such a file directly (for example with ./script.sh), the shebang line usually fails with a message such as /usr/bin/env: 'bash\r': No such file or directory, and other lines report $'\r': command not found. The fix is to convert the line endings. Using sed:

sed -i 's/\r$//' script.sh

This substitution removes a carriage return at the end of every line and edits the file in place. After conversion, the script prints hello normally. Many editors can save with LF line endings, and Git can be configured to avoid converting them. Setting your editor to use LF for shell scripts prevents the issue.

Running Windows Programs from Bash

Interoperability works in both directions. From Bash, you can run Windows executables by name, including the .exe extension:

cmd.exe /c ver
notepad.exe notes.txt

You can also pipe between Windows and Linux programs. This lets you combine Linux text tools with Windows applications. One caution: Windows programs expect Windows-style paths, so convert paths with wslpath -w when you pass file names to them.

Using an Editor with WSL

Many editors integrate with WSL. Visual Studio Code, for example, has an extension that lets you open a folder located in the Linux file system and run its terminal and tools inside WSL, while the editor window itself stays on Windows. From a Bash prompt in a project folder you can start it with:

code .

This works when the editor and its WSL integration are installed.

Where WSL Bash Differs from Native Linux

WSL gives you real Linux tools, but a few differences matter:

  • Services that depend on a full init system may need extra configuration.
  • Network ports opened in WSL are normally reachable from Windows, although the details depend on the WSL version and settings.
  • Files on /mnt/c use Windows permissions, so chmod may have little effect there.
  • Time, memory, and disk are shared with Windows according to the WSL configuration.

For learning Bash scripting, none of these get in the way. WSL is a full-featured place to run every example in this tutorial.

Alternatives on Windows

WSL is the recommended route, but other options exist:

  • Git Bash, installed with Git for Windows, provides a Bash emulation with common Unix tools. It is lighter than WSL but is not a full Linux system.
  • Virtual machines or dual boot give a complete Linux installation, at the cost of more setup.
  • Containers can provide a Bash prompt inside a Linux image.

Choose WSL when you want the closest experience to a real Linux machine without leaving Windows.

0 Comments

Reviewed before they appear

No comments yet.

Bash
Introduction to Bash What Is a Shell? What Is Bash (Bourne Again Shell)? Bash vs. Shell vs. Terminal Bash vs. sh, Zsh, and Fish Where Bash Is Used: Linux, DevOps, and Automation Installing Bash on Linux Using Bash on macOS Using Bash on Windows with WSL Checking Your Bash Version Opening and Using a Terminal Running Your First Bash Command Understanding Command Syntax and Arguments Bash Built-in Commands vs. External Commands Working with pwd, ls, and cd Creating Files and Directories with touch and mkdir Copying, Moving, and Renaming Files Removing Files and Directories Safely Viewing Files with cat, less, head, and tail Searching for Files with find Searching Text with grep Understanding File Paths and Wildcards Using man Pages and Command Help Bash Script Structure Creating Your First Bash Script Understanding the Shebang (#!/usr/bin/env bash) Making a Script Executable with chmod Running a Bash Script in Different Ways Writing Comments in Bash Using echo and printf Reading User Input with read Displaying Helpful Messages in Scripts Using Semicolons and Newlines Bash Variables: Declaration and Usage Rules for Naming Bash Variables Environment Variables Local Variables vs. Environment Variables Exporting Variables with export Common Environment Variables: HOME, PATH, USER, and PWD Read-Only Variables with readonly Removing Variables with unset Command-Line Arguments: $0, $1, and More Special Parameters: $#, $?, $$, $!, $@, and $* Positional Parameters and shift Default Values with Parameter Expansion Single Quotes vs. Double Quotes Escaping Special Characters Command Substitution with $(...) Arithmetic Expansion with $((...)) Brace Expansion Tilde Expansion and Home Directories Filename Expansion and Globbing Understanding Word Splitting Parameter Expansion Basics Why Quoting Variables Matters Standard Input, Standard Output, and Standard Error Redirecting Output with > and >> Redirecting Input with < Redirecting Errors with 2> Combining Standard Output and Standard Error Using Pipes (|) to Connect Commands Using tee to Save and Display Output Here Documents with << Here Strings with <<< Command Lists with ;, &&, and || Conditional Statements with if, then, and fi Using elif and else Testing Conditions with test and [ ] Using [[ ]] for Bash Conditionals Comparing Strings in Bash Checking Whether Files Exist Checking File Types and Permissions Integer Comparisons in Bash Logical Operators: &&, ||, and ! Using case Statements Pattern Matching in case Common Conditional Statement Mistakes for Loops in Bash C-Style for Loops while Loops until Loops Looping Through Files and Directories Looping Through Command Output Safely break and continue Nested Loops select Loops for Simple Menus Avoiding Infinite Loops Defining and Calling Bash Functions Passing Arguments to Functions Returning Status Codes from Functions Understanding return vs. echo in Functions Local Variables in Functions Function Scope and Variable Visibility Organizing Scripts into Functions Sourcing Other Bash Files with source Reusable Bash Utility Functions Indexed Arrays in Bash Creating and Accessing Array Elements Adding and Removing Array Elements Looping Through Arrays Array Length and Indices Associative Arrays in Bash Iterating Over Associative Arrays Passing Arrays to Functions Common Array Pitfalls Processing Text with grep, sed, and awk Using cut, sort, uniq, and wc Finding and Replacing Text with sed Extracting Columns with awk Parsing CSV-Like Text from the Command Line Working with Filenames That Contain Spaces Safe File Handling with find -print0 and xargs -0 Temporary Files with mktemp Reading Files Line by Line Appending Logs to a File Understanding Exit Codes Checking Command Success with $? Using exit to End a Script Fail-Fast Scripts with set -e: Benefits and Caveats Using set -u and set -o pipefail Combining Strict Mode Options Safely Handling Errors with if Statements Traps with trap Cleaning Up Temporary Files on Exit Writing Useful Error Messages Debugging Scripts with bash -x Parsing Script Arguments Validating Required Arguments Handling Optional Arguments Parsing Options with getopts Building Help Messages with --help Setting Defaults for Optional Parameters Handling Unknown Options and Invalid Input Creating Interactive Menus in Bash Foreground and Background Processes Running Commands in the Background with & Managing Jobs with jobs, fg, and bg Waiting for Processes with wait Capturing Process IDs with $! and $$ Sending Signals with kill Handling Interrupts and Termination Signals Running Commands with timeout Basic Process Monitoring from Bash Aliases and Shell Functions: When to Use Each Customizing the Bash Prompt (PS1) Bash Startup Files: .bashrc, .bash_profile, and Profile Interactive vs. Non-Interactive Bash Shell Options with set shopt: Enabling Bash-Specific Options Directory Stack with pushd, popd, and dirs Process Substitution with <(...) and >(...) Advanced Parameter Expansion Pattern Matching and Extended Globbing Bash Script Security Fundamentals Avoiding Command Injection in Shell Scripts Quoting User Input Safely Why eval Is Risky and Safer Alternatives Handling Secrets and Credentials in Scripts Using Least Privilege in Automation Scripts Safe Use of rm and Destructive Commands Validating Paths and User-Supplied Filenames ShellCheck: Static Analysis for Bash Scripts Writing Readable and Maintainable Bash Automating Backups with Bash Creating Log Rotation and Cleanup Scripts Checking Disk Usage and Free Space Monitoring CPU, Memory, and System Load Checking Website Availability with curl Automating Updates on Ubuntu and Debian Scheduling Bash Scripts with cron Using Lock Files to Prevent Duplicate Runs Building a Simple Health-Check Script Sending Notifications from Bash Scripts Using Bash in Git Workflows Bash Scripts for Docker Automation Automating Docker Container Health Checks Using Bash in CI/CD Pipelines Environment Configuration for Deployment Scripts Automating SSH Tasks with Bash Deploying a Web Application with a Bash Script Using Bash to Process Application Logs Building Repeatable Development Setup Scripts Bash Debugging Techniques Using set -x and PS4 for Debug Traces Testing Bash Scripts with Bats Mocking Commands During Script Tests Testing Error Paths and Exit Codes Writing Portable Shell Scripts Bash-Specific Syntax vs. POSIX Shell Syntax Using ShellCheck in a Development Workflow Performance Tips for Bash Scripts Build an Interactive Calculator Build a File and Directory Organizer Build a Timestamped Backup Script Build a Log File Analyzer Build a Bulk File Renaming Tool Build a System Resource Monitoring Script Build a Website Uptime Checker Build a User Account Audit Report Build a Database Backup Automation Script Build a Docker Cleanup Helper Build a Server Health-Check and Alert Script Build a Deployment Automation Script Build a Menu-Driven Linux Administration Toolkit Build a Safe Automated Backup and Restore Workflow
Ask about this post
AI Ask about this post

Ask questions about Using Bash on Windows with WSL and get answers drawn from it.

Signed-in readers only.