Powershell是否具有与bash子外壳等效的功能? [英] Does Powershell have an equivalent to the bash subshell?

查看:124
本文介绍了Powershell是否具有与bash子外壳等效的功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Linux bash shell中真正很棒的一件事是,您可以在子shell中定义变量,并且在该子shell完成定义的(环境?)变量之后,只要您定义它们而不导出它们,并且在子shell中,该变量就不存在了.

One thing that's really great in linux bash shell is that you can define variables inside of a subshell and after that subshell completes the (environment?) variables defined within are just gone provided you define them without exporting them and within the subshell.

例如:

$ (set bob=4)
$ echo $bob
$

不存在变量,因此没有输出.

No variable exists so no output.

我最近也在写一些powershell脚本,并注意到我一直不得不在脚本末尾清空变量/对象;在powershell中使用等效的subshel​​l可以清除此问题.

I was also recently writing some powershell scripts and noticed that I kept having to null out my variables / objects at the end of the script; using a subshell equivalent in powershell would clear this up.

推荐答案

我以前从未听说过这种功能,但是通过运行以下命令可以获得相同的效果:

I've not heard of such functionality before, but you can get the same effect by running something like the following:

Clear-Host
$x = 3
& {
    $a = 5
    "inner a = $a"
    "inner x = $x"
    $x++
    "inner x increment = $x"
}
"outer a = $a"
"outer x = $x"

输出:

inner a = 5
inner x = 3
inner x increment = 4
outer a = 
outer x = 3

这篇关于Powershell是否具有与bash子外壳等效的功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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