Running .sh Files in WSL2 on Windows 10: A Simple Guide

Abstract: Resolve permission errors when executing shell scripts in WSL2 by using bash or chmod +x.

Created: 2026-08-06 by
To run a shell script in Windows Subsystem for Linux 2, open your WSL terminal and navigate to the directory containing the file. Use the command bash filename.sh to execute it directly, or make the file executable with chmod +x filename.sh and run it using ./filename.sh. This approach ensures that your Linux-based automation tasks function correctly within the WSL2 environment on Windows 10.

Prerequisites and Environment Setup

Before attempting to execute any shell scripts, you must ensure that the Windows Subsystem for Linux is properly installed and configured. WSL2 provides a full Linux kernel inside Windows, allowing you to run native Linux binaries and scripts without virtualization overhead. If you have not yet installed WSL2, you can do so by opening PowerShell as an administrator and running the command wsl --install. After installation, restart your computer to apply the changes. Once restarted, open your preferred Linux distribution from the Start menu, such as Ubuntu. You will be greeted with a terminal session that behaves exactly like a standard Linux shell. It is important to understand that while Windows uses the NTFS file system, WSL2 accesses these files through a mounted drive at /mnt/c/. This path structure is crucial when navigating between Windows directories and Linux-native directories.

Navigating to Your Script Location

Shell scripts are often stored in your Windows user folders, such as Documents or Desktop. To access these files from within WSL2, you must use the correct mount path. For example, if your script is located in C:\Users\YourName\Documents\script.sh, you would navigate to it in the WSL terminal using:
cd /mnt/c/Users/YourName/Documents
Replace YourName with your actual Windows username. Once you are in the correct directory, you can list the contents using ls -l to verify that the .sh file is present. Understanding this path mapping is essential because trying to run a script from a relative path without being in the correct directory will result in a "No such file or directory" error. For users who frequently switch between Windows and Linux environments, keeping scripts in the Linux home directory (~) is often more convenient than storing them in the Windows partition. This avoids potential permission issues and improves performance, as accessing files within the ext4 virtual disk is faster than crossing the filesystem boundary. If you need to manage network shares or remote resources, you might also look into Accessing RDP Shares in Linux WSL for advanced connectivity options.

Executing Scripts Using Bash

The most straightforward way to run a shell script is by explicitly invoking the bash interpreter. Even if your script has the .sh extension, Windows does not inherently know how to handle it. By typing bash scriptname.sh, you tell the system to use the bash shell to interpret the commands inside the file. This method works regardless of whether the script has execution permissions set. This approach is particularly useful for testing scripts quickly or when you do not want to modify the file permissions. It ensures that the script runs with the default settings of the bash shell, which is usually what you expect in a Linux environment. However, note that each time you use this method, the shell starts a new instance to process the file, which can be slightly slower for very large scripts compared to direct execution.

Making Scripts Executable

A more professional and efficient workflow involves making your scripts executable. This allows you to run them directly by typing their name, similar to running native applications. To do this, you first need to change the file permissions using the chmod command. In the terminal, navigate to the directory containing your script and run:
chmod +x scriptname.sh
The +x flag adds the execute permission for the owner, group, and others. Once this is done, you can run the script simply by entering ./scriptname.sh. The dot-slash prefix is required to indicate that you are running the file in the current directory. Without it, the shell will search for the command in your system's PATH, which typically does not include the current directory for security reasons. It is worth noting that some scripts may require specific shebang lines at the top, such as #!/bin/bash, to define the interpreter. If you encounter errors after making a file executable, check the first line of the script to ensure it points to the correct shell binary. For more complex scripting needs involving data synchronization, understanding tools like rsync can be beneficial, as seen in Understanding the '-t' Option in rsync.

Creating a Batch File Wrapper

If you prefer to launch your Linux scripts from the Windows Command Prompt or PowerShell instead of the WSL terminal, you can create a batch file wrapper. Create a new text file named run_script.bat and add the following line:
wsl bash /mnt/c/Users/YourName/Documents/scriptname.sh
Save this file and double-click it whenever you need to run the script. This method bridges the gap between Windows UI workflows and Linux backend processes. It is especially handy for developers who want to trigger deployment scripts or build processes with a simple click from their desktop.

Frequently Asked Questions

Why do I get a 'Permission denied' error?

This error occurs when the script lacks execute permissions. You must run chmod +x filename.sh to grant execution rights before using ./filename.sh.

Can I run .sh files directly from Windows Explorer?

No, Windows cannot natively execute Linux shell scripts. You must use the WSL terminal or a batch wrapper to invoke the bash interpreter.

What is the difference between bash script.sh and ./script.sh?

bash script.sh explicitly calls the interpreter and ignores the shebang line, while ./script.sh relies on the file's permissions and the shebang to determine the interpreter.

Do I need to install additional software to run shell scripts?

No, WSL2 comes with bash and other standard Linux utilities pre-installed. As long as WSL2 is active, you can run standard POSIX-compliant scripts.

How do I debug a failing shell script?

Run the script with bash -x scriptname.sh to print each command as it executes. This verbose mode helps identify syntax errors or missing variables.

Latest News

How to Display Output of xterm Command in Terminal Screen using Cron

Resolve the inability to display xterm output in a terminal screen from cron by redirecting logs or using wall instead of insecure X11 workarounds.

Read More
Expose Local Server on VPS Preserving Real Client IPs (OS Level)

Resolve VPS logging showing only gateway IPs by configuring reverse proxies to forward real client addresses via X-Forwarded-For headers.

Read More
How to Clear Recent Files History in Linux without Deleting Actual Files?

Clear recent files history in Linux by deleting ~/.local/share/recently-used.xbel without removing the actual files.

Read More
Best HDD Copy Strategy: Manually Syncing and Rewriting Scratch

Resolve data drift between two 4TB hard drives by using rsync with the --delete flag to manually sync and rewrite scratch data for an exact mirror.

Read More
Understanding Bash Prompt Abbreviations: An Overview

Resolve repetitive typing errors by creating and persisting Bash aliases in the .bashrc file for improved command-line efficiency.

Read More
Free Tools and Methods to Log Real-Time Performance Details in Task Manager

Log real-time CPU and memory metrics to CSV files using Windows Performance Monitor Data Collector Sets to troubleshoot system slowness.

Read More
Contact Qatar Airways Customer Service: Phone Number, Help Center, and More

Resolve urgent Qatar Airways reservation issues like flight changes and baggage claims using the US phone number +1801-855-5905 or online self-service tools.

Read More
Windows 11: Alt Codes Displaying Wrong Characters - Solutions

Resolve Windows 11 Alt codes showing wrong characters by verifying the US keyboard layout and enabling Num Lock.

Read More
Run Programs with Elevated Privileges: A Comprehensive Guide

Eliminates the need to type sudo for frequent administrative tasks by configuring .bashrc aliases or functions.

Read More
Dynamically Changing Dates in Excel Formulas Using Cells

Eliminate manual date updates in Excel by using cell references to create dynamic formulas that automatically adjust calculations when input dates change.

Read More
Granting Edit Access to a Specific Excel Sheet in an Office 365 User Email ID-Based Login

Excel Online lacks native sheet-level permissions, so users must split workbooks or use worksheet protection to isolate edit access for specific tabs.

Read More
Creating Network Load on Local Wi-Fi from One PC

Generate sustained Wi-Fi bandwidth load from a single PC using a VM SFTP server to test router stability and throughput.

Read More
Changing the Default Editor for Batch Files in Windows 11's File Explorer

Resolve the issue of batch files opening in Notepad by changing the default editor to a preferred application like VS Code.

Read More
Gain WiFi Connection Minutes Before Losing it by Changing MAC Address

Bypass MAC address filtering to restore WiFi connectivity temporarily when a device is blocked by network administrators.

Read More
Photos Folders Turned Black: Troubleshooting Picture Applications on Windows

Resolve black screens in Windows Photos by resetting the app, updating graphics drivers, and repairing system files.

Read More
Troubleshooting High CPU Temperature Values: A Step-by-Step Guide

Resolve high CPU temperatures and thermal throttling by monitoring with Speedfan, cleaning dust from cooling components, and replacing degraded thermal paste.

Read More
Make cURL establish TLS connection with CA unknown

Resolve cURL TLS connection failures with unknown or self-signed certificates by using the --insecure flag to bypass CA validation.

Read More
Troubleshooting Incorrect Network Metrics Values in Grafana, Prometheus, InfluxDB, and Telegraf

Fix incorrect network metrics in Grafana, Prometheus, InfluxDB, and Telegraf by verifying interface selection, resolving unit mismatches, and handling counter resets.

Read More
Start Another Terminal Window (cmd.exe) Windows Repair/Troubleshoot

Provides step-by-step instructions to access the Windows Command Prompt via bootable media for advanced system repair and troubleshooting.

Read More
Thinkpad P1 Gen 6: Sudden Thunderbolt 3 Dock USB-C Charging Stopped Working

Resolve sudden Thunderbolt 3 dock USB-C charging and internet failure on ThinkPad P1 Gen 6 by updating firmware, resetting the dock, and adjusting power settings.

Read More
ASUS Windows 11 Laptop: Internet Connection Issue with Hotspot

Resolve ASUS Windows 11 laptop spinning wheel issue when connecting to mobile hotspot by resetting network stack, disabling IPv6, and adjusting power settings.

Read More
Developing iOS Project with Firebase: Logging in and Sending Push Notifications

Integrates Firebase to enable secure user authentication and push notifications in iOS applications.

Read More
Troubleshooting Bash Script with Suspected Issues of Spaces, Dashes, Filenames and Quoting

Resolve Bash script failures caused by word splitting, globbing, and special characters in filenames by applying proper quoting techniques.

Read More
Populating OneDrive Folder for Local Backup without Affecting Remote Files

Prevent OneDrive from deleting remote files during Windows reinstallation by unlinking the account or using symbolic links to isolate local data.

Read More
Force First Frame as Keyframe Without Re-encoding: A Quick Solution

Resolve inaccurate seeking and playback glitches by re-encoding video to ensure the first frame is a valid keyframe.

Read More
Find Information in Database Using WSL (Ubuntu) on Windows-10

Resolve database connection issues from WSL Ubuntu on Windows 10 by configuring client tools and network access to query remote or local servers.

Read More
Configuring a New Router with Different Networks: One Family, One Guest

Resolve IP conflicts and isolate guest traffic by configuring a second router with a distinct subnet via its WAN port.

Read More
Quickly Open Multiple Stock Websites with a Single Command

Solve the problem of manually opening multiple stock research websites by creating a single command to launch them simultaneously on Windows, macOS, or Linux.

Read More
Improved Adobe Acrobat Search Experience: Access Advanced Search with Ease

Access advanced search filters like case sensitivity and regex in Adobe Acrobat by clicking More Options in the Find bar.

Read More
Port Forwarding VM via OpenVPN: A Comprehensive Guide

Configure OpenVPN server and iptables to forward specific ports from VPN clients to a virtual machine's internal IP address.

Read More
Understanding Partitions in the 1980s: A Look Back at MS-DOS and Master Boot Record

Explains how the Master Boot Record and partition table enabled MS-DOS to manage multiple disk volumes on a single physical drive.

Read More
Windows 10 Enterprise: Keyboard Not Detected in Managed Organization with Admin Privileges

Resolve Windows 10 Enterprise keyboard detection failures caused by Group Policy restrictions, driver corruption, or USB enumeration errors.

Read More
Can't Find x86_64 Versions of Applications on MacOS arm64 (M1)

Resolve missing x86_64 app issues on M1 Macs by installing Rosetta 2 and reinstalling apps as universal binaries.

Read More
How to Hide Installation Source for an Android App

Manage Android app installation source visibility by using USB transfers or file managers to reduce browser traces and isolate data via work profiles.

Read More
Connecting Momentus 4200.2 ST9808210A Hard Drive via USB Cable

Resolve inability to access Seagate Momentus 4200.2 ST9808210A hard drive by connecting it via USB using a compatible SATA-to-USB adapter and configuring the disk in Windows.

Read More