在 PowerShell (7) 上,可以使用以下命令运行 Linux 命令:wsl grep 'test'
。
如何运行涉及管道(在 Linux/WSL 上)和空格的更复杂的命令?
例如:
wsl find . -name '*.txt' | xargs grep 'main('
这会导致错误:
xargs: The term 'xargs' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
尽管xargs
在 WSL 内部显然可用。
这种变化比如将另一个放在wsl
前面xargs
会导致以下错误:
wsl find . -name '*.txt' | wsl xargs grep 'main('
/bin/bash: -c: line 1: syntax error near unexpected token `('
/bin/bash: -c: line 1: `xargs grep main('
ResourceUnavailable: Program 'wsl.exe' failed to run: The pipe is being closed.At line:1 char:1
+ wsl find . -name '*.txt' | wsl xargs grep 'main('
+ ~~~~~~~~~~~~~~~~~~~~~~~~.
请注意,我想直接从 PowerShell 执行此操作,而不是先启动 WSL 终端,然后在其中运行 Linux 命令(这显然有效)。