-1

I have a command

lsof -i :3000

When I run it, I got this

⚡️lsof -i :3000                                                                                                             
COMMAND  PID  USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME                                                              
node    6803 bheng   13u  IPv6 0xa757c4ba1a0bf589      0t0  TCP *:hbci (LISTEN)                                                   

I want to create an alias of this command that takes a parameter as my port number.

I'm not looking to create a function or anything just for this.


I've tried

alias checkport='lsof -i :$1'

I want to use it like this

checkport 3000

Result

⚡️checkport 3000                                                                                                            
lsof: unacceptable port specification in: -i :                                                                                    
lsof 4.89                                                                                                                         
 latest revision: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/                                                                 
 latest FAQ: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/FAQ                                                                   
 latest man page: ftp://lsof.itap.purdue.edu/pub/tools/unix/lsof/lsof_man                                                         
 usage: [-?abhlnNoOPRtUvV] [+|-c c] [+|-d s] [+D D] [+|-f[cgG]]                                                                   
 [-F [f]] [-g [s]] [-i [i]] [+|-L [l]] [+|-M] [-o [o]] [-p s]                                                                     
 [+|-r [t]] [-s [p:s]] [-S [t]] [-T [t]] [-u s] [+|-w] [-x [fl]] [--] [names]                                                     
Use the ``-h'' option to get more help information.                                                                               
Community
  • 1
  • 1
code-8
  • 54,650
  • 106
  • 352
  • 604

1 Answers1

3

Aliases don't take parameters. Define a function instead.

checkport () {
    lsof -i ":$1"
}

However, because the "argument" to the alias is simply appended to the end of the alias expansion, you could also define

alias checkport='lsof -i :'

Both would be used the same way: checkpoint 3000.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Ohh.. how do I use it after define that function ? – code-8 Nov 28 '17 at 16:20
  • You use functions the same way you would any command: `checkpoint 3000`. – chepner Nov 28 '17 at 16:20
  • where is that function should go ? inside a certain file somewhere ? Can you elaborate ? – code-8 Nov 28 '17 at 16:21
  • 1
    You would define it wherever you were planning to define the alias. – chepner Nov 28 '17 at 16:21
  • in the .bash_profile , got it. – code-8 Nov 28 '17 at 16:22
  • `.bash_profile` is typically a worse choice than `.bashrc`, unless you're `export`ing the function (as with `export -f checkport`). And since you *can't* export an alias, `.bash_profile` is a bad choice for them in general. – Charles Duffy Nov 28 '17 at 16:22
  • even on a Mac ? – code-8 Nov 28 '17 at 16:23
  • Also, is the function name much match the alias name ? – code-8 Nov 28 '17 at 16:23
  • On a Mac, your new terminals will evaluate `.bash_profile`, but that doesn't mean other shells they start will do so as well; whereas *all* interactive bash instances source `.bashrc`. – Charles Duffy Nov 28 '17 at 16:24
  • "must match the alias name"? Eh? We're advising to use a function *instead of* an alias, not in addition to. – Charles Duffy Nov 28 '17 at 16:24
  • Where should I add my aliases, so I don't have to open up a new sessions to be able to access those new aliases ? – code-8 Nov 28 '17 at 16:25
  • For this function: `.bash_profile`, but put `export -f checkport` after the definition. – Charles Duffy Nov 28 '17 at 16:27
  • I'd put them in `.bashrc` (and don't export) instead of `.bash_profile`; that ensures they get sourced for any interactive shell without wasting space in the environment. – chepner Nov 28 '17 at 16:28
  • @CharlesDuffy Do I need to run `export -f checkport` on a cli or need to add it after my function ? – code-8 Nov 28 '17 at 16:28
  • ...the caveat there is that MacOS is a bit weird, and one might need to `[ -r ~/.bashrc ] && . ~/.bashrc` or such from `.bash_profile` there to get `.bashrc` consistently executed. – Charles Duffy Nov 28 '17 at 16:29
  • @ihue, bottom line, there are other questions about how to do this, and multiple possible answers with some disagreement -- consider finding such a question and evaluating its answers and their comment threads to make your own decision. :) – Charles Duffy Nov 28 '17 at 16:29
  • I am very confuse, I don't which one of you, I shold listen too now. :D. Both of you seems very knowledgable about bash. – code-8 Nov 28 '17 at 16:30
  • @CharlesDuffy - I guess, I will do that. Thanks for advise. :) – code-8 Nov 28 '17 at 16:30