13

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.

6
  • 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, 2017 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, 2017 at 11:31
  • 1
    Related: How to pass parameters to an alias? Aug 6, 2017 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, 2017 at 11:56
  • @JoeHealey Added more details, hope it helps.
    – Putnik
    Aug 6, 2017 at 12:37

1 Answer 1

14

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.

1
  • Good idea! Would it there be a way to use also provide the autocompletion facility we get using alias?
    – MensSana
    Aug 13, 2022 at 21:49

Not the answer you're looking for? Browse other questions tagged .