How to create a function in bashrc to accept arguments?

CommonCents picture CommonCents · Mar 4, 2013 · Viewed 12.7k times · Source

I would like to have an alias in my bashrc file to append the argument passed to it from terminal. For example:

$ lh300

calls:

alias lh3000='open http://localhost:3000'

However, if I type:

$ lh8080 or lh followed by any number:

$ lh#### 

I would like to call a function that appends the #### into an alias that will

'open http://localhost:####'

How can I do this?

Answer

chrisaycock picture chrisaycock · Mar 4, 2013

You won't be able to use an alias, but you can create a function:

lh() { open http://localhost:$1; }

Then just call it like lh 3000.