Passing a port number into an alias

907 views Asked by At

I am trying to create an alias for the following command

lsof -i :$1 | awk 'NR!=1 {print $2}' | xargs kill

However I keep getting an error:

lsof: unacceptable port specification in: -i :

I tried different things but I keep failing to pass the port number successfully. How can I pass the port number into this alias?

1

There are 1 answers

2
fedorqui On BEST ANSWER

If you need to pass a parameter into a specific position, an alias cannot be done. Instead, create a function:

myfunc() {
   lsof -i :$1 | awk 'NR!=1 {print $2}' | xargs kill
}

If you save it in your ~/.bashrc file you will be able to call it with myfunc parameter.