views:

44

answers:

2

Is there a good way to do multiple substitutions for aliasing a command?

For example

alias cmd = 'ssh -R $1:$2:$1:$2 $3 | something {$1, $2, $3}'
cmd 127.0.0.1 1234 server

Something like this..

Actually, this doesn't really make any sense to pipe the output like this, but similar syntax is what I'd like to see.

It's be nice to have named mappings too, but just indexes is fine.

Using awk perhaps?

+4  A: 

How about using a shell function instead?:

$ cmd() { echo ssh -R $1:$2:$1:$2 $3 ; echo something {$1, $2, $3} ; }
$ cmd 127.0.0.1 1234 server
ssh -R 127.0.0.1:1234:127.0.0.1:1234 server
something {127.0.0.1, 1234, server}
Ned Deily
Or a straight-forward shell script - you're gonna need to load the function from somewhere, sometime, but unless you're doing this operation an awful lot, the cost of using a script over a function is going to be minimal (and the benefit is that you don't load the script if you don't use it).
Jonathan Leffler
@Jonathan - true, but the OP asked for an alias, and a shell function is closer to that than a shell script. The same common-practice applies to defining functions and aliases - put it in your .bashrc or whatever.
Stephen C
+1  A: 

You have to define it by using a function. Example:

cmd () { echo -e "$1\n$2" | grep "$1"; }

Don't forget the space between { and echo.

This would result in the following behaviour:

$ cmd hello world
hello
Jonatan Lindén

related questions