4

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?

Hrvoje T
  • 1,031

2 Answers2

5

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:

igal
  • 9,886
  • 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
  • The path /home/user/bin is designed for user scripts. You may add this. –  Nov 14 '17 at 22:22
  • @Arrow I didn't know that. Is there a reference for that? – igal Nov 14 '17 at 22:23
  • It is a function of how $PATH is built read this. –  Nov 14 '17 at 22:26
  • 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 06:14
  • So just to clarify: there's no way to dump what's already defined into ~/.bashrc ( or any other file for that matter ) ? Manual way only? – Sergiy Kolodyazhnyy Nov 15 '17 at 08:33
  • @SergiyKolodyazhnyy I'm not sure what you mean by "dump" what's in ~/.bashrc. I wouldn't say that ~/.bashrc is the "manual way" of doing anything, since it's run automatically (under the appropriate conditions). – igal Nov 15 '17 at 13:10
  • @SergiyKolodyazhnyy Actually, maybe I do understand what you're asking. If you want to add a previously declared function to your ~/.bashrc you could use the type or declare -f commands. They will output the function definition. So you could do something like declare -f myfunction >> ~/.bashrc to append the function definition for myfunction to the ~/.bashrc file. – igal Nov 15 '17 at 13:16
0

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.

user1934428
  • 707
  • 5
  • 15