简体   繁体   English

在Mac终端中使用变量创建别名

[英]Create an alias with a variable in Mac Terminal

How do you use variables in an alias? 如何在别名中使用变量? For example if pbcopy will allow me to access the clipboard (copied ip address) and assign it to $ip I could copy an IP address and then use an alias such as the below failed example. 例如,如果pbcopy将允许我访问剪贴板(复制的IP地址)并将其分配给$ip我可以复制IP地址,然后使用alias ,如下面的失败示例。

alias go='ip=pbpaste | ssh $ip -l pete'

我通常使用pbpaste / pbcopy ,但您可能希望找到具有历史记录的东西供您使用。

无需创建变量或管道,只需使用子shell:

alias go='ssh $(pbpaste) -l pete'

Another option is to use a function in place of an alias, as functions can take arguments: 另一种选择是使用函数代替别名,因为函数可以接受参数:

function go {
    ssh "$1" -l pete
}

After copying the IP address, you can type "go ", paste the IP address with Command-V, and hit return. 复制IP地址后,可以键入“go”,使用Command-V粘贴IP地址,然后单击“返回”。

You can also duplicate the accepted answer to avoid having to paste the address manually: 您还可以复制接受的答案,以避免手动粘贴地址:

function go {
    ssh $(pbpaste) -l pete
}

so the real message here is to start thinking in terms of shell functions instead of aliases, since according to the bash man page 所以这里真正的信息是开始考虑shell函数而不是别名,因为根据bash手册页

For almost every purpose, aliases are superseded by shell functions. 对于几乎所有目的,别名都被shell函数取代。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM