这是一个奇怪的。通常,当我从powershell执行外部命令时,我使用&
运算符,如下所示:
& somecommand.exe -p somearguments
但是,今天我遇到了像这样使用的
.
运算符:.$env:systemdrive\chocolatey\chocolateyinstall\chocolatey.cmd install notepadplusplus
在此情况下,此期间有什么作用?我不明白
最佳答案
“。” dot sourcing operator将发送和接收来自您调用的其他脚本的变量。 “&” call operator将仅发送变量。
例如,考虑以下内容:
脚本1(call-operator.ps1):
clear
$funny = "laughing"
$scriptpath = split-path -parent $MyInvocation.MyCommand.Definition
$filename = "laughing.ps1"
"Example 1:" # Call another script. Variables are passed only forward.
& $scriptpath\$filename
"Example 2:" # Call another script. Variables are passed backwards and forwards.
. $scriptpath\$filename
$variableDefinedInOtherScript
脚本2(laughing.ps1):# This is to test the passing of variables from call-operator.ps1
"I am $funny so hard. Passing variables is so hilarious."
$variableDefinedInOtherScript = "Hello World!"
创建两个脚本,仅运行第一个脚本。您会看到“。”点源运算符发送和接收变量。两者都有用途,所以要有创造力。例如,如果您想修改另一个脚本中变量的值,同时又保留当前脚本中的原始值,则“&”调用运算符将很有用。金田有保障。 ;)
关于powershell - 在Powershell中,句点 '.'运算符有什么作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727006/