wt executable with the --wsl flag followed by your specific command string. This approach leverages Windows Terminal's native ability to spawn new panes or windows directly into a Linux subsystem environment, allowing for automated scripting or quick access to development tools without manual navigation through the user interface.
Understanding the WT Executable
The primary tool for this task is the wt command-line utility, which is installed alongside Windows Terminal. It serves as a launcher that can open new tabs, split panes, or entire windows based on JSON profiles or direct arguments. When interacting with Ubuntu or other WSL distributions, you must specify the profile name or use flags to ensure the shell initializes correctly before running your script.
For most users, the simplest method involves invoking wt from PowerShell, Command Prompt, or even the Run dialog. The key is understanding that wt does not just open a window; it manages the lifecycle of the terminal instances. By passing arguments directly, you bypass the need to locate the correct profile in the settings menu every time.
Basic Syntax for Immediate Execution
To run a Bash command immediately upon startup, you should use the following structure in your command line:
wt --wsl bash -c "your_command_here"
This command tells Windows Terminal to open a new instance (or tab, depending on your default settings) targeting the WSL subsystem. The bash -c portion instructs the Bash interpreter to execute the string provided in quotes as a single command. This is crucial because it ensures the command runs in a non-interactive shell context, which is necessary for automation and immediate execution without waiting for user input.
If you have multiple WSL distributions installed, you may need to specify the exact distribution name instead of relying on the default. You can do this by replacing --wsl with --profile "Ubuntu-20.04", assuming that is the name defined in your settings.json. Always verify your profile names by checking the configuration file or running wt --list-profiles.
Handling Complex Commands and Arguments
When your Bash command includes spaces, special characters, or multiple commands chained together, proper quoting is essential. For example, if you want to navigate to a directory and list files, you might use:
wt --wsl bash -c "cd ~/projects && ls -la"
Note that the outer quotes define the argument passed to wt, while the inner logic is handled by Bash. If your command itself contains double quotes, you should escape them or use single quotes for the outer wrapper to prevent shell interpretation errors. For instance:
wt --wsl bash -c 'echo "Hello World"'
This technique allows for robust scripting where Windows Terminal acts merely as the container for your Linux environment. You can chain any valid Bash command, including Git operations, Python scripts, or system administration tasks. For more advanced scenarios involving Git Bash specifically, see How to Start Windows Terminal and Run Git Bash Command Immediately.
Customizing the Window Behavior
By default, wt opens a new window. However, you can modify this behavior using additional flags. To open the command in a new tab within an existing window, add the --new-tab flag:
wt --new-tab --wsl bash -c "npm start"
To split the pane horizontally or vertically, use --split horizontal or --split vertical. This is particularly useful for developers who want to keep their main workflow visible while running background processes or monitoring logs in a secondary pane. The command would look like this:
wt --split-vertical --wsl bash -c "tail -f /var/log/syslog"
These options provide flexibility in how the terminal session integrates with your current workspace. You can combine these flags to create complex layouts that suit your multitasking needs. Remember that if no window exists, wt will always launch a new one regardless of split flags, but subsequent calls will adhere to the specified layout rules.
Creating Persistent Shortcuts
For frequent use, consider creating a batch file or PowerShell alias to avoid typing long commands repeatedly. Create a file named run_bash.bat with the following content:
@echo off
wt --wsl bash -c "%*"
You can then call this script from anywhere in your system by simply typing run_bash echo hello. This abstracts away the complexity of the wt syntax and provides a clean interface for executing Linux commands from the Windows ecosystem. Alternatively, you can add an alias in your PowerShell profile for a more integrated experience.
If you are interested in opening the preview version of the terminal programmatically, check out Open Windows Terminal PREVIEW from command line for details on managing different versions of the application.
Frequently Asked Questions
Why does my command fail with a "command not found" error?
This usually happens because the WSL path is not included in the PATH variable when invoked via wt. Ensure you are calling the full path to the binary or sourcing your .bashrc file first. Adding source ~/.bashrc before your command often resolves environment variable issues.
Can I run this command without opening a new window?
No, Windows Terminal is designed to manage distinct sessions. Even if you use --new-tab, it requires an active terminal instance. There is no way to inject a command into an already open terminal window directly from the command line without using inter-process communication tools like conpty or third-party plugins.
Does this work with PowerShell Core inside Windows Terminal?
Yes, but you must change the profile target. Instead of --wsl, use --profile "PowerShell" and replace bash -c with powershell -Command. The syntax remains similar, but the shell interpreter changes to handle PowerShell-specific commands.
How do I pass arguments with spaces to the Bash command?
Enclose the entire command string in single quotes if it contains double quotes, or escape internal double quotes. For example: wt --wsl bash -c 'echo "file with spaces.txt"'. This prevents the outer shell from breaking the argument into separate parts.
Is there a performance penalty for launching wt frequently?
Launching wt is lightweight compared to starting a full GUI application, but it still incurs the overhead of initializing the WSL VM. For rapid-fire commands, consider keeping a persistent terminal window open and using Ctrl+Shift+C/V to copy-paste commands manually, or use a multiplexer like tmux inside WSL.