I have this function to get MAC address from IP:

ip2arp()  {
   local ip="$1"
   ping -c1 -w1 "$ip" >/dev/null
   arp -n "$ip" | awk '$1==ip {print $3}' ip="$ip"
   }

What is the right way for using it later? Save to /usr/bin as sh and make it executable, or save it in a home directory and make an alias in bash? Is there a right and wrong way?

up vote 3 down vote accepted

If it's only for your personal use then you could add it to your shell's initialization file as a function, e.g. ~/.bashrc.

For a summary of the different initialization files in Bash you can consult the Bash Guide for Beginners:

Also see the Bash Reference Manual:

A typical pattern would be to put your function definition in your ~/.bashrc file and source that file from your ~/.bash_profile.

But it's probably worth noting that which profile file to use can depend on your OS, your terminal application, and your own preferences. See for example the following posts on AskDifferent:

Also see this post on StackOverflow:

Alternatively, you can create a personal directory for your own scripts (e.g. I use ~/local/bin) and then add that directory to your PATH in your profile file (i.e. export PATH="${HOME}/local/bin:${PATH}).

If you want to make it available to other users then you might put it in /usr/local/bin as a script (rather than /usr/bin).

For further discussion regarding where to put executable files, see the following posts:

  • 4
    ~/.profile is to configure your login session, not your shell. Only the login shell reads that file. bash customisation goes in ~/.bashrc – Stéphane Chazelas Nov 14 '17 at 21:53
  • 1
    @StéphaneChazelas Sorry, Mac user here. Thanks for the comment. I updated my solution. Is that better? – igal Nov 14 '17 at 22:12
  • I saved it in .bashrc. Had to reopen a terminal to load that config. Now it works, thanks to both very much – Hrvoje T Nov 14 '17 at 22:19
  • 1
    @HrvojeT If you have the time and the inclination, you might want to create a temporary user and populate all of their shell initialization and profile files with echo statements, and then open shell windows in various ways, e.g. launch your terminal application, open a new tab, open a new window, run a command in a subshell, etc. That way you can watch and see the order in which the different files are loaded. I remember finding that very instructive. – igal Nov 14 '17 at 22:21
  • 1
    @Arrow Hmmm. I don't see anything there that says that ~/bin has been designated for user scripts, except by recent versions of Ubuntu. – igal Nov 15 '17 at 6:14

If you know for sure, that you are using this function only from the command line, you can of course place it into your initialization files.

If you plan to use it from other shell scripts too, you have to ways to go:

  • You can organize your programming environment around libraries consisting of one or more function definitions, which you source from those shell scripts which need them, or

  • You drop the idea of using functions and make executable scripts out of it.

As long as your functions are not supposed to manipulate shell variables, it's up to you which way to go. Using a separate, executable script instead of a function, has of course the advantage that it can be executed from any other program, not only from bash.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.