是“美元符号"吗?PowerShell 中的可选“$()"? [英] Is "Dollar-sign" optional in powershell "$()"?
问题描述
我已经看到了一个使用 powershell 设置文件时间戳(创建时间、上次访问时间、上次写入时间)的示例:
I've seen an example for setting the timestamps (creationtime, lastaccesstime, lastwritetime) of a file with powershell:
PS>$(get-item test.txt).lastwritetime=$(get-date "01/01/2020")
这似乎有效.
此页面:http://ss64.com/ps/syntax-operators.html 关于 powershell 操作符说 "$( )"
是一个SubExpression 操作符".
This page: http://ss64.com/ps/syntax-operators.html about powershell operators says that "$( )"
is a "SubExpression operator".
但它似乎也可以在没有 "$"
的情况下工作,例如:
But it also seems to work without the "$"
like:
PS>(get-item test.txt).lastwritetime=(get-date "01/01/2020")
和我见过的大多数 powershell 示例在使用括号时都省略了 "$"
.
and most powershell examples I've seen omit the "$"
when using parenthesis.
那么,powershell "$()"
中的美元符号"是可选的吗?或者有/没有我没有看到的 "$"
有什么区别.
So, is the "Dollar-sign" optional in powershell "$()"
? Or is there some difference with/without the "$"
that I'm just not seeing.
"$()"
和 "()"
实际上是 2 个不同的运算符,它们恰好在我展示的示例中都有效使用吗?
Is "$()"
and "()"
actually 2 different operators that just happen to both be valid uses in the examples I have shown?
推荐答案
如果您使用多个语句或语句嵌入在字符串中,则需要 $ 符号来表示子表达式.没有 $ 的括号只是一个分组运算符.
You need the $ sign to denote a sub expression if you use multiple statements or the statement is embedded in a string. Parenthesis without the $ is just a grouping operator.
$($x = 1; $y =2; $x + $y).tostring() //3
($x = 1; $y =2; $x + $y).tostring() // invalid syntax
($x = 1 + 2).tostring() //3
"$(1 +2)" //3
这篇关于是“美元符号"吗?PowerShell 中的可选“$()"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!