如何为变量设置速记别名? [英] How can I set a shorthand alias for a variable?
问题描述
我正在使用一个设置 $Global:LastResult
的脚本,我如何设置别名,比如 last
来访问这个变量
I am using a script that sets $Global:LastResult
, how can I set an alias, say, last
to access this variable
推荐答案
你不能为变量定义别名(好吧,技术上你可以,只要变量不是 $null
,但是您的别名将具有赋值时变量的值).
You can't define an alias for a variable (well, technically you can, as long as the variable isn't $null
, but then your alias would have the value of the variable at the time of the assignment).
您可以做的是定义一个函数,该函数返回 $global:LastResult
的值,然后是该函数的别名:
What you can do is define a function that returns the value of $global:LastResult
and then an alias for that function:
function Get-LastResult { $global:LastResult }
New-Alias -Name last -Value Get-LastResult
然而,我没有看到这样的方法比直接使用变量 $global:LastResult
的优势.
However, I fail to see the advantage of an approach like this over directly using the variable $global:LastResult
.
这篇关于如何为变量设置速记别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!