What does script: do in powershell? -
i've seen syntax on variable before , not quite sure is:
$script:foo = "bar"
the syntax $script:foo
commonly used modify script-level variable, in case $foo
. when used read variable, $foo
sufficient. example rather write this:
verbose-script.ps1 $script:foo = '' function f { $script:foo }
i write (less verbose , functionally equivalent):
script.ps1 $foo = '' function f { $foo }
where $script:foo
crucial when want modify script-level variable within scope such function or anonymous scriptblock e.g.:
ps> $f = 'hi' ps> & { $f; $f = 'bye';$f } hi bye ps> $f hi
notice $f
outside scriptblock did not change though modified bye
within scriptblock. happened modified local copy of $f
. when don't apply modifier script:
(or global:
), powershell perform copy-on-write
on higer-scoped variable local variable same name.
given example above, if wanted make permanent change $f
, use modifier script:
or global:
e.g.:
ps> $f = 'hi' ps> & { $f; $global:f = 'bye';$f } hi bye ps> $f bye
Comments
Post a Comment