gpt4 book ai didi

powershell - $a = $(Read-Host) 和 $a = (Read-Host) 有什么区别?

转载 作者:行者123 更新时间:2023-12-02 17:27:21 28 4
gpt4 key购买 nike

我必须查看我的前同事留下的一些脚本,我很好奇为什么他在参数中使用 $a = $(Read-Host -Prompt "Write Something")节而不是 $a = (Read-Host -Prompt "Write Something")

我已经在单个脚本中对其进行了测试,但到目前为止还没有看到任何差异。我已经通过谷歌和 StackOverFlow 进行了搜索,但到目前为止还没有找到任何线索可能有什么区别。也许我的搜索方法不好。

最佳答案

引用自Keith Hill's blog :

What’s the difference between grouping expressions (), subexpressions $() and array subexpressions @()? A grouping expression can contain just a single statement. A subexpression can contain multiple semicolon separated statements. The output of each statement contributes to the output of the subexpression. An array subexpression behaves just like a subexpression except that it guarantees that the output will be an array. The two cases where this makes a difference are 1) there is no output at all so the result will be an empy array and 2) the result is a scalar value so the result will be a single element array containg the scalar value. If the output is already an array then the use of an array subexpession will have no affect on the output i.e. array subexpressions do not wrap arrays inside of another array.

愚蠢的例子:

$a = (Read-Host -Prompt 'something'; echo 'foo')
$b = $(Read-Host -Prompt 'something'; echo 'foo')

第一个语句(带有分组表达式)将引发错误,因为分组表达式中不能有多个语句。第二条语句将起作用,并将一行“foo”附加到通过 Read-Host 输入的文本中。

在您的示例场景中 ($a = (Read-Host -Prompt 'something')$a = $(Read-Host -Prompt 'something') >) 没有任何区别。更准确地说,在这种情况下您不应使用分组表达式或子表达式运算符,因为它们在那里没有任何用途。

关于powershell - $a = $(Read-Host) 和 $a = (Read-Host) 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44926780/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com