How shall I reuse a function in multiple scripts?

In bash, sometimes I would like to reuse a function in several scripts.
Is it bad to repeat the definition of the function in all the scripts?
If so, what is some good practice?

Is the following way a good idea?

  • wrap the definition of a function myfunction in its own script define_myfunction.sh,
  • in any script where the function is called, source define_myfunction.sh , and then call the function myfunction arg1 arg2 ....

Thanks.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

In bash, that is a good way of doing it, yes.

Sourcing the “library” script using source or . would be an adequate solution to your issue of wanting to share a function definition between two or more scripts. Each script that needed access to the function(s) defined in the “library” script(s) would source the needed file(s), probably at the top of the script.

This would allow you to collect related functions in a single “library” script and source it to get access to them.

Not entirely unrelated, but the bash shell also has the ability to automatically source a file upon executing a non-interactive shell (i.e. a script). This may be used to set up a specific environment for the script:

BASH_ENV="$HOME/stuff/script.env" ./myscript

… where script.env may do anything from defining functions and setting shell or environment variables, to sourcing other files etc.


Some shells, like ksh93, has an environment variable that points to a directory that may contain function definition scripts like these. In ksh93, this variable is called FPATH. A script, $FPATH/foo, would contain a function called foo, which would automatically be found when the user types foo on the command line (or in a script). The bash shell does to my knowledge not have this specific functionality.

Other shells have other forms of “auto-load” functionality.

Method 2

This question may get flagged for being subjective, but I believe it’s generally accepted as best practice to use libraries for commonly-used code rather than reinventing the wheel every time. sourceing a script which defines functions and variables is the shell way to do this, so I would say yes, having library scripts and sourceing them is probably good practice.

I actually go one further and have a bin directory in my PATH with tools and script I frequently use; some of those library scripts are in there so I can source them readily for use at my interactive shell.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x