20

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?

CommonCents
  • 315
  • 1
  • 3
  • 7

2 Answers2

27

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.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
3

A questionable hack involving command_not_found_handle:

command_not_found_handle () {
    if [[ $1 =~ lh([[:digit:]]+) ]]; then
        open "http://localhost:$BASH_REMATCH[1]"
    fi
}

This requires bash 4 or later, I believe.

chepner
  • 497,756
  • 71
  • 530
  • 681