user1541542
user1541542

Reputation: 59

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.

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

Upvotes: 2

Views: 5039

Answers (3)

chepner
chepner

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

Andrew Marshall
Andrew Marshall

Reputation: 96904

No need to create a variable or pipe, simply use a subshell:

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

Upvotes: 3

LeGBT
LeGBT

Reputation: 104

I usually use pbpaste/pbcopy, but you may want to find something with a history for your uses.

Upvotes: 4

Related Questions