How to Run a Linux Command from a Batch Script via WSL
One of the most powerful features of the Windows Subsystem for Linux (WSL) is its seamless integration with the Windows command line. You don't have to manually "open" a Linux terminal to perform a task; you can trigger a Linux command directly from a standard Windows Batch script. This allows you to combine the best of both worlds: using Batch for Windows file management and administrative tasks, while calling Linux utilities like grep, sed, awk, or python3 for complex data processing.
This guide explains how to use the wsl command to execute Linux tasks without leaving your .bat environment.
Why Combine Batch and Linux?
- Powerful Parsing: Using
grepandsedto process text files is significantly easier and faster than using complex Batch string manipulation. - Cross-Platform Tooling: Running a Linux-based deployment script or linter as part of a Windows build process.
- Unified Automation: Creating a single script that handles both Windows service restarts and Linux database cleanup.
The standard command is: wsl <command>. By default, this runs the command in your default distribution using the current Windows directory as the starting path.
Method 1: Running a Single Command (Direct)
You can call any Linux binary just like a standard Windows command.
@echo off
echo [PROCESS] Listing files using Linux 'ls -la'...
wsl ls -la
pause
Method 2: Running a Command in a Specific Distribution
If you have multiple versions of Linux installed (e.g., Ubuntu and Kali), you can specify which one should execute the command using the -d flag.
@echo off
echo [PROCESS] Checking Debian version...
wsl -d Debian cat /etc/os-release
pause
Method 3: Capturing Linux Output into a Batch Variable
This is the most "integrated" way to use WSL. You can run a complex Linux command and store its result back into a Windows variable for further use in your script.
@echo off
setlocal
:: Use a FOR loop to capture the output of a Linux command
:: In this example, we get the current Linux user
for /f "tokens=*" %%i in ('wsl whoami') do set "L_USER=%%i"
echo The current WSL user is: %L_USER%
pause
Creating a Hybrid Automation Script
The following script uses Linux to find "large files" in a Windows folder and then uses Batch to report the result.
@echo off
setlocal
echo ============================================================
echo Hybrid Disk Auditor (Batch + Linux^)
echo ============================================================
:: 1. Check that WSL is available
where wsl.exe >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] WSL is not installed. Cannot run Linux commands.
pause
exit /b 1
)
:: 2. Define target directory (converted for WSL)
:: Windows C:\Users becomes /mnt/c/Users
set "W_PATH=C:\Temp"
:: Convert the Windows path to a Linux path using wslpath
for /f "tokens=*" %%p in ('wsl wslpath "%W_PATH%"') do set "L_PATH=%%p"
echo [PROCESS] Finding files larger than 50MB in %W_PATH%...
echo [INFO] Linux path: %L_PATH%
echo.
:: 3. Use Linux 'find' as it is much faster and more flexible than 'dir'
wsl find "%L_PATH%" -type f -size +50M -exec ls -lh {} ;
if %errorlevel% neq 0 (
echo [ERROR] Linux command failed. Check that the path exists.
)
echo ============================================================
pause
Common Pitfalls and How to Avoid Them
Path Format Confusion (The /mnt factor)
Windows uses C:\ but Linux uses /mnt/c/. If you pass a Windows path directly to a Linux command, it will fail.
Wrong Way:
wsl cat C:\test.txt
:: Result: "cat: C:test.txt: No such file or directory"
Correct Way:
Use the wslpath utility inside your script to convert paths automatically:
for /f "tokens=*" %%p in ('wsl wslpath "C:\test.txt"') do set "L_PATH=%%p"
wsl cat "%L_PATH%"
Quote and Pipe Escape Issues
Linux commands often use pipes (|) and special characters that Batch might try to interpret itself before sending them to WSL.
If your Linux command includes a pipe or special characters, use wsl bash -c "your command here" to ensure the entire command string is passed to the Linux shell for interpretation rather than being parsed by Batch:
wsl bash -c "cat /etc/os-release | grep PRETTY_NAME"
Best Practices for Hybrid Scripting
- Check for WSL Availability: Always verify
wsl.exeexists before trying to run commands, or your script will crash for users without Linux installed. - Use the Right Distro: If you need a specific version of a tool (like
Python 3.10), specify the distro explicitly (wsl -d Ubuntu ...) rather than relying on the "Default." - Root Privileges: If your Linux command requires
sudo, you should configure your Linux user for "Nopasswd" in thevisudofile, otherwise the Batch script will hang indefinitely waiting for a password input you cannot see.
When you run wsl <command>, it automatically starts in the same folder as your Batch script. This makes it incredibly easy to process files in the current directory using Linux power-tools.
Conclusion
Running Linux commands from a Batch script via WSL is one of the most effective ways to upgrade your Windows automation environment. By leveraging the advanced text processing and utility power of Linux alongside the administrative control of Batch, you can solve complex problems with significantly less code. This professional, hybrid approach to scripting ensures that you always have the right tool for the job, making your automation projects more efficient, portable, and capable.