bashcommand-line-argumentsaliasport-number

Passing a port number into an alias


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?


Solution

  • 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.