引用别名作为函数中的变量
Reference alias as variable in function
我正在设置我的 PowerShell 配置文件,并想将我用于文本编辑器的别名定义为一个变量,这样如果我将我的配置文件移植给其他人,他们就可以随意定义别名.
以下是我个人资料的前几行:
$textEditorAlias = 'np'
$textEditorExecutable = 'notepad++.exe'
set-alias -name $textEditorAlias -value (get-command $textEditorExecutable).path -scope Global -Option AllScope
# Shortcuts to commonly used files.
function frequentFile { $textEditorAlias 'C:\<pathToFile>\fileName' }
我的问题是上面的函数 returns“表达式或语句中出现意外标记”。
如果我将函数替换为
function frequentFile { np 'C:\<pathToFile>\fileName' }
然后就可以了。
有没有办法让变量 $textEditorAlias 在函数表达式中干净地展开?
谢谢。
PowerShell 中的别名非常简单 - AliasName -> CommandName
- 否
参数,没有自定义,只是简单的名称到名称的映射。
这意味着您不需要显式调用 Get-Command
- PowerShell 会自动为您完成:
$textEditorAlias = 'np'
$textEditorExecutable = 'notepad++.exe'
Set-Alias -Name $textEditorAlias -Value $textEditorExecutable
My problem is the function above returns "unexpected token in expression or statement".
如果要调用基于字符串变量的命令,请使用 &
调用运算符:
$textEditorAlias = 'np'
function frequentFile { & $textEditorAlias 'C:\<pathToFile>\fileName' }
我正在设置我的 PowerShell 配置文件,并想将我用于文本编辑器的别名定义为一个变量,这样如果我将我的配置文件移植给其他人,他们就可以随意定义别名.
以下是我个人资料的前几行:
$textEditorAlias = 'np'
$textEditorExecutable = 'notepad++.exe'
set-alias -name $textEditorAlias -value (get-command $textEditorExecutable).path -scope Global -Option AllScope
# Shortcuts to commonly used files.
function frequentFile { $textEditorAlias 'C:\<pathToFile>\fileName' }
我的问题是上面的函数 returns“表达式或语句中出现意外标记”。
如果我将函数替换为
function frequentFile { np 'C:\<pathToFile>\fileName' }
然后就可以了。
有没有办法让变量 $textEditorAlias 在函数表达式中干净地展开?
谢谢。
PowerShell 中的别名非常简单 - AliasName -> CommandName
- 否
参数,没有自定义,只是简单的名称到名称的映射。
这意味着您不需要显式调用 Get-Command
- PowerShell 会自动为您完成:
$textEditorAlias = 'np'
$textEditorExecutable = 'notepad++.exe'
Set-Alias -Name $textEditorAlias -Value $textEditorExecutable
My problem is the function above returns "unexpected token in expression or statement".
如果要调用基于字符串变量的命令,请使用 &
调用运算符:
$textEditorAlias = 'np'
function frequentFile { & $textEditorAlias 'C:\<pathToFile>\fileName' }