PowerShell 中的点 (.) 和与号 (&) 有什么区别? [英] What is the difference between dot (.) and ampersand (&) in PowerShell?

查看:113
本文介绍了PowerShell 中的点 (.) 和与号 (&) 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PowerShell 中,调用 cmdlet、函数、脚本文件或可运行程序时,使用点 (.) 和与号 (&) 有什么区别?

例如:

<预><代码>.foo.sh1&foo.sh1

<小时>

有一个非常相似的问题被错误地关闭为重复:调用 PowerShell 脚本块时与号 (&) 和点 (.) 之间的区别.这些问题是不同的,并且具有完全不同的关键字和搜索排名.PowerShell 管道中的`.` 简写是什么? 只回答了一半的问题.

解决方案

  • .& 运算符之间的区别在调用脚本时很重要> 或 函数(或它们的别名) - 对于 cmdlet外部程序,它们的作用相同.

  • 对于脚本和函数,.&定义的范围方面有所不同>函数别名变量:

    • &调用运算符,在作用域内执行脚本和函数,也就是典型用例:通常期望函数和脚本执行没有副作用:

      • 在脚本/调用的函数中定义的变量、(嵌套)函数、别名对于调用来说是局部的,并且在脚本退出/函数返回时超出范围.

      • 但是请注意,即使是在范围内运行的脚本也会影响调用者的环境,例如使用Set-Location 更改当前位置,显式修改父作用域 (Set-Variable -Scope 1 ...) 或全局作用域 ($global:...) 或定义进程级环境变量.

    • .点源操作符,在当前范围内执行脚本和函数,并且通常用于修改调用者的作用域,方法是添加函数、别名和可能的变量供以后使用.例如,此机制用于加载初始化交互式会话的 $PROFILE 文件.

需要注意的是,对于 functions(相对于脚本)来说,childcurrent 的引用范围不一定是 调用者的作用域:如果函数是在模块中定义的,则引用作用域是那个模块的作用域域:

  • 换句话说:尝试将 .module-originated 函数一起使用几乎毫无意义,因为被修改的范围是 module 的.
  • 也就是说,无论如何,模块中定义的函数在设计时通常不会考虑点源.

In PowerShell, what is the difference between using dot (.) and ampersand (&) when invoking a cmdlet, function, script file, or operable program?

For example:

. foo.sh1
& foo.sh1


There is a very similar question which has been incorrectly closed as a duplicate: Differences between ampersand (&) and dot (.) while invoking a PowerShell scriptblock. The questions are different and have completely different keywords and search rankings. The answer on What is the `.` shorthand for in a PowerShell pipeline? only answers half the question.

解决方案

  • The difference between the . and & operators matters only when calling scripts or functions (or their aliases) - for cmdlets and external programs, they act the same.

  • For scripts and functions, . and & differ with respect to scoping of the definition of functions, aliases, and variables:

    • &, the call operator, executes scripts and functions in a child scope, which is the typical use case: functions and scripts are typically expected to execute without side effects:

      • The variables, (nested) functions, aliases defined in the script / function invoked are local to the invocation and go out of scope when the script exits / function returns.

      • Note, however, that even a script run in a child scope can affect the caller's environment, such as by using Set-Location to change the current location, explicitly modifying the parent scope (Set-Variable -Scope 1 ...) or the global scope ($global:...) or defining process-level environment variables.

    • ., the dot-sourcing operator, executes scripts and functions in the current scope and is typically used to modify the caller's scope by adding functions, aliases, and possibly variables for later use. For instance, this mechanism is used to load the $PROFILE file that initializes an interactive session.

The caveat is that for functions (as opposed to scripts) the reference scope for child vs. current is not necessarily the caller's scope: if the function was defined in a module, the reference scope is that module's scope domain:

  • In other words: trying to use . with a module-originated function is virtually pointless, because the scope getting modified is the module's.
  • That said, functions defined in modules aren't usually designed with dot-sourcing in mind anyway.

这篇关于PowerShell 中的点 (.) 和与号 (&amp;) 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆