Reputation: 59
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.
alias go='ip=pbpaste | ssh $ip -l pete'
Upvotes: 2
Views: 5039
Reputation: 530823
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.
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
For almost every purpose, aliases are superseded by shell functions.
Upvotes: 4
Reputation: 96904
No need to create a variable or pipe, simply use a subshell:
alias go='ssh $(pbpaste) -l pete'
Upvotes: 3
Reputation: 104
I usually use pbpaste/pbcopy, but you may want to find something with a history for your uses.
Upvotes: 4