I am not showing much code here , but I have a requirement to fix. We have file with over 100 function that are being used in various shell scripts in production. We have named it as.


Up-till now each script call this "functions.sh" file in it so it can access the updated function. Like this

# comment
# comment
# Comment


echo "Date" > logging()

Now we have been asked to get rid of this call :


from scripts that will be developed in future. (so that more such files can be added) & make sure latest functions are automatically refreshed in environment as soon as new "functions.sh" is deployed to production and any scripts can use the function defined in them directly . Shall I just call it in .bashrc file as


or is there anything else we have to do , keeping in mind scripts will be executed in non-interactive mode through crontab. ( all scripts are bash scripts ) any help is appreciated.


  • A title like "best way" trends dangerously towards asking for opinions. Can you find another way to phrase it?
    – Jeff Schaller
    Apr 21, 2020 at 12:45
  • @jeff any suggestion from your experience
    – ETL_Devs
    Apr 21, 2020 at 12:46
  • I can't make out what your question/problem is, yet.
    – Jeff Schaller
    Apr 21, 2020 at 12:48
  • @JeffSchaller Updated Question , what Can I do to make it clear. I am in a real situation here.
    – ETL_Devs
    Apr 21, 2020 at 12:50
  • 1
    Your point about being run via cron isn't a minor thing, that kind of changes everything. Please edit your question and tell us exactly how you will run these scripts that need the functions and also how you define the functions and if they are exported. Also, how do you load the functions? Did you really mean you run ~/.functions.sh or did you actually mean . ~/.functions.sh? We would also need to know your OS since some details may change.
    – terdon
    Apr 21, 2020 at 12:52

1 Answer 1


Read https://www.gnu.org/software/bash/manual/bash.html#Bash-Startup-Files

For non-interactive shells (such as running a script via cron), bash uses the BASH_ENV environment variable as the name of a file to source.

So, for maximum flexibility, you can do something like this:

├── functions.d
│   ├── func1.sh
│   ├── func2.sh
│   ├── ...
│   └── funcN.sh
└── functions.sh

And functions.sh could be

#!/usr/bin/env bash
if [[ -d "$func_dir" ]]; then
    for f in "$func_dir"/*.sh; do
        if [[ -r "$f" ]]; then
            . "$f"

In your crontab file, add


So all you have to do is add a .sh file to that directory, and your cron scripts will source it.

This gives you the flexibility to create files each containing just a single function definition. No more giant library files.

  • thanks < I will try and update it here.
    – ETL_Devs
    Apr 21, 2020 at 14:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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