powershell - 在Powershell中,句点 '.'运算符有什么作用?

标签 powershell syntax operators

这是一个奇怪的。通常,当我从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/

相关文章:

powershell - 从AssemblyInfo.cs获取并替换AssemblyVersion

powershell - 使用 Powershell 中的另一个列表在列表中搜索部分匹配项

javascript - javascript语法错误Windows主机行1字符1

c# - ILSpy 中显示的奇怪的类成员语法

ruby - 什么是带上下文的 Ruby 方法定义?

operators - 什么是Prolog中的->运算符,该如何使用?

.net - 从Powershell表单文本框中删除字符限制

c++ - 如何从 C 代码调用 powershell 脚本

java - 将中缀转换为后缀时识别负值

c# - 如何验证一个类型是否重载/支持某个运算符?