向 .zshrc 添加函数会更改命令

adding a function to .zshrc changes the command

我将以下功能添加到我的 .zshrc

function jptt(){
  # Forwards port  into port  and listens to it
  ssh -N -f -L localhost::localhost: remoteuser@remotehost
}

那我运行宁jptt 1 2

并得到以下错误:

Bad local forwarding specification localhost:2ocalhost:1

很奇怪我在2

之后输了:l

当我尝试用简单的一行替换命令并且它起作用时,该功能正在运行。我还单独 运行 ssh 命令,它运行良好。

表达式 $x:l 将 lower-casing modifier 应用于您的 x 变量。以下示例说明了这一点:

pax> x=ABC
pax> echo $x:lnnn
abcnnn

pax> echo ${x}:lnnn
ABC:lnnn

第一部分为您提供 lower-case 变体,因此修饰符不被视为输出字符串的一部分。第二部分展示了如何通过使用大括号来确保 :l 不是 而不是 被视为修饰符来防止此变量扩展。在您的特定情况下,这将通过以下行完成:

ssh -N -f -L localhost::localhost: remoteuser@remotehost

养成尽可能多地使用参数名称的习惯实际上是个好主意,因为在 其他 情况下这可能会对您产生不利影响。

使用 </code> 和 <code> 。 Zsh 支持:csh 字符串修饰符和 :l 具有特殊含义(在它前面的小写变量),这就是为什么它从 :l[=14= 中消耗的原因].