With WSL 1, the output of wsl.exe --help
includes the following usage information:
Usage: wsl.exe [Argument] [Options...] [CommandLine]
Arguments to run Linux binaries:
If no command line is provided, wsl.exe launches the default shell.
--exec, -e <CommandLine>
Execute the specified command without using the default Linux shell.
Therefore, the difference between wsl.exe foo
and wsl.exe --exec foo
is as follows:
wsl.exe foo
launches the default shell configured for your WSL distribution and passes the commandfoo
to it to be executed.wsl.exe --exec foo
executes the commandfoo
directly, without involving the default shell.
For example, if the default shell for your WSL distribution is bash
, you can use wsl.exe --exec fish
to run the fish
shell directly, without involving bash
.
Both wsl.exe foo
and wsl.exe --exec foo
can be used to run the foo
command in your WSL distribution. However, wsl.exe --exec foo
allows you to bypass the default shell and execute the command directly.
To further illustrate the difference between wsl.exe foo
and wsl.exe --exec foo
, consider the following example:
wsl.exe -d Ubuntu fish
wsl.exe -d Ubuntu --exec fish
Both of these commands will start the fish
shell in the Ubuntu
WSL distribution. However, the first command will start the bash
shell (which is the default shell for Ubuntu
) and then run the fish
command within it, while the second command will start the fish
shell directly, without involving the default shell.
You can use the ps
command to see the processes that are running in your WSL distribution. If you run ps
after starting the fish
shell using either of the above commands, you should see fish
and ps
as the only processes listed. This is because fish
is the only process that was started, regardless of whether it was started directly or through the default shell.
So, what is the purpose of the --exec
option?
3 Answers
Understanding the ‘—exec’ Option in WSL
Windows Subsystem for Linux (WSL) is a compatibility layer that allows users to run Linux applications on Windows. WSL is a powerful tool that allows developers and system administrators to use Linux tools and utilities on Windows without the need for a virtual machine or dual-boot setup. One of the features of WSL is the --exec
option, which allows users to execute Linux commands without using the default Linux shell. In this post, we will explore the --exec
option in WSL and its benefits.
What is the ‘—exec’ Option in WSL?
The --exec
option in WSL is used to execute Linux commands directly, without involving the default Linux shell. When you run a command in WSL, it is typically passed to the default Linux shell configured for your distribution. However, with the --exec
option, you can bypass the default shell and execute the command directly.
For example, if the default shell for your WSL distribution is bash
, you can use the --exec
option to run the fish
shell directly, without involving bash
. This can be useful if you want to run a specific command without having to first start the default shell.
How is the ‘—exec’ Option Different from Running a Command Normally?
The main difference between running a command normally and using the --exec
option is that when you run a command normally, it is passed to the default Linux shell for your distribution. The default shell then executes the command. However, when you use the --exec
option, the command is executed directly, without involving the default shell.
For example, if you run the command wsl.exe foo
, the default shell for your distribution will be started, and the foo
command will be passed to it to be executed. However, if you run the command wsl.exe --exec foo
, the foo
command will be executed directly, without involving the default shell.
Benefits of Using the ‘—exec’ Option
There are several benefits to using the --exec
option in WSL. One of the main benefits is that it allows you to execute commands more quickly, as you are not first required to start the default shell. This can be particularly useful if you need to run a command quickly or if you are working with large datasets that require a lot of processing power.
Another benefit of using the --exec
option is that it allows you to run commands that may not be supported by the default shell. For example, if your default shell is bash
, but you need to run a command that is only supported by the zsh
shell, you can use the --exec
option to run the command directly in the zsh
shell.
Examples of Using the ‘—exec’ Option
Here are some examples of using the --exec
option in WSL:
wsl.exe --exec ls -l
: This command will execute thels -l
command directly, without involving the default shell.wsl.exe --exec python script.py
: This command will execute thescript.py
Python script directly, without involving the default shell.wsl.exe --exec fish
: This command will start thefish
shell directly, without involving the default shell.
Conclusion
The --exec
option in WSL is a powerful tool that allows users to execute Linux commands directly, without involving the default Linux shell. This option can be particularly useful if you need to run a specific command quickly or if you are working with large datasets that require a lot of processing power. By using the --exec
option, you can bypass the default shell and execute commands more quickly and efficiently.
The purpose of the --exec
option in WSL (Windows Subsystem for Linux) is to allow you to execute a command directly, without involving the default shell. This can be useful if you want to run a command that is not available in the default shell, or if you want to bypass the shell altogether.
For example, suppose you have a WSL distribution with bash as the default shell, and you want to run the fish shell. You can use the following command to do so:
wsl.exe --exec fish
This will start the fish shell directly, without involving bash. You can also use the --exec
option to run any other command that is available in your WSL distribution, such as ls
, cat
, or grep
.
It is worth noting that the --exec
option is only available in WSL 1. In WSL 2, the wsl.exe
command does not have an --exec
option, and all commands are run directly without involving the default shell.
I hope this information was helpful! If you have any more questions about the --exec
option in WSL or anything else, feel free to ask.
The wsl.exe command allows you to execute a command in the default Linux shell of your default WSL distribution. However, when you use the –exec or -e option, the command is not passed to the shell. Instead, it is executed directly without any shell processing.
For example, the command “wsl.exe $(which ls)” will be processed by the shell, but “wsl.exe –exec $(which ls)” will not because the $() syntax is not processed by any shell.
This behavior is the same for both WSL1 and WSL2. To see this in action, you can use the Process Monitor tool as an administrator and add a filter for “Path – Contains – bash – Include”. Then, run a WSL1 distribution with and without the –exec option. You will see that the bash process does not appear when using the –exec option.
Note that Process Monitor cannot catch processes running in a VM/container, such as those in WSL2.
Here are my final thoughts:
- Using the –exec or -e option allows you to execute a command directly without passing it through the default shell. This can be useful if you want to bypass any shell processing or if you want to run a command that is not recognized by the shell.
- Both WSL1 and WSL2 support the –exec or -e option, and the behavior is the same in both versions.
- You can use the Process Monitor tool to see the difference in how the command is executed with and without the –exec or -e option. However, be aware that Process Monitor cannot catch processes running in a VM/container, such as those in WSL2.
I hope this helps! Let me know if you have any other questions.