如何输入多行命令 [英] How to enter a multi-line command
问题描述
是否可以将PowerShell命令行分成多行?
Is it possible to split a PowerShell command line over multiple lines?
在Visual Basic中,我可以使用下划线(_
)在下一行继续该命令.
In Visual Basic I can use the underscore (_
) to continue the command in the next line.
推荐答案
您可以在空格后面加上重音符号(反引号):
You can use a space followed by the grave accent (backtick):
Get-ChildItem -Recurse `
-Filter *.jpg `
| Select LastWriteTime
但是,仅在上述情况下才需要这样做.通常,当命令在语法上无法完全完成时,您会获得自动的行连续性.这包括启动一个新的管道元素:
However, this is only ever necessary in such cases as shown above. Usually you get automatic line continuation when a command cannot syntactically be complete at that point. This includes starting a new pipeline element:
Get-ChildItem |
Select Name,Length
将正常工作,因为在|
之后,该命令由于缺少另一个管道元素而无法完成.同样,打开花括号或任何其他种类的括号也可以直接继续行:
will work without problems since after the |
the command cannot be complete since it's missing another pipeline element. Also opening curly braces or any other kind of parentheses will allow line continuation directly:
$x=1..5
$x[
0,3
] | % {
"Number: $_"
}
类似于|
的逗号在某些情况下也可以使用:
Similar to the |
a comma will also work in some contexts:
1,
2
请紧记,类似于JavaScript的自动分号插入,有些情况也类似,因为换行符发生在 前面带有有效语句的位置:
Keep in mind, though, similar to JavaScript's Automatic Semicolon Insertion, there are some things that are similarly broken because the line break occurs at a point where it is preceded by a valid statement:
return
5
不起作用.
最后,字符串(在所有变体中)也可能会超出一行:
Finally, strings (in all varieties) may also extend beyond a single line:
'Foo
bar'
然后,它们在字符串中包含换行符.
They include the line breaks within the string, then.
这篇关于如何输入多行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!