Skip to main content
deleted 9 characters in body
Source Link
Tim
  • 107k
  • 234
  • 651
  • 1.1k

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 the script, and then call the function myfunction arg1 arg2 ....

Thanks.

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 the script and then call the function myfunction arg1 arg2 ....

Thanks.

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.

Source Link
Tim
  • 107k
  • 234
  • 651
  • 1.1k

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 the script and then call the function myfunction arg1 arg2 ....

Thanks.