3

This question already has an answer here:

We can do with ease:

myalias='command param1 param2'

then myalias param3 runs

command param1 param2 param3

I need an alias which will run the same, but without the last 'space':

command param1 param2param3

For example I have many servers in /24 subnet and need to run the same command using the ip. So I want alias like command param1 192.168.0. then just run myalias 103 and the command will be applied to 192.168.0.103.

I tried to add $1 at the end of the alias but no luck.

marked as duplicate by Jeff Schaller, cas, Rui F Ribeiro, Stephen Rauch, Community Aug 6 '17 at 17:03

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 3
    Sorry, aliases are fairly limited. As the Bash docs state: "For almost every purpose, aliases are superseded by shell functions." – PM 2Ring Aug 6 '17 at 11:24
  • 1
    For anything even vaguely complicated, functions make much more sense. Aliases really are just aliases, i.e. renaming an existing command.. – Joe Healey Aug 6 '17 at 11:31
  • 1
    Related: How to pass parameters to an alias? – steeldriver Aug 6 '17 at 11:51
  • Do you have a non dummy example you could post because I'm not sure what practical purpose passing 2 parameters without the space actually serves? – Joe Healey Aug 6 '17 at 11:56
  • @JoeHealey Added more details, hope it helps. – Putnik Aug 6 '17 at 12:37
3

If you are happy to use a function, you can do something like:

command(){
"$1" "$2" "192.168.0.${3}"
}

If I test this with a dummy command:

mycommand(){
$1 "130.200.68.${2}"
}

$ mycommand ping 180

Would ping the server with the last section of the IP added.

Not the answer you're looking for? Browse other questions tagged or ask your own question.