执行的PowerShell脚本中的“ CD”命令仍然会影响当前会话

发布于 2025-01-25 17:00:07 字数 308 浏览 4 评论 0原文

来自UNIX World,执行(而不是来源)的脚本将在子壳中运行,因此可变声明和cd命令不会影响实际的当前会话(我们键入命令的会话)。

参考:

# Executing a script
./myscript.ps1

# Sourcing a script
. ./myscript.ps1

比PowerShell相比,非常惊讶(坦率地说)很烦人(坦率地说),执行脚本仍然会影响我有关cd命令的当前会话(但没有声明的变量)。

我如何防止这种情况发生?

Coming from the Unix world, scripts when executed (as opposed to sourced) are run in a subshell, and thus variable declarations and cd commands do not affect the actual current session (the one where we type our commands).

For reference:

# Executing a script
./myscript.ps1

# Sourcing a script
. ./myscript.ps1

Very surprised (and quite frankly annoyed) to see than in Powershell, executing a script will still affect my current session regarding cd commands (but not declared variables).

How can I prevent this from happening ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文