Bash function defined in .bashrc not available to other scripts

Solution 1:

As others have stated, you need to define your functions when loading your script.

I'm doing this by having a script file that contains my "common" functions that I use in scripts in general. Let's call this file /path/to/bash-common.sh - and in your case, this script would contain the function load_module.

Then what I'm doing is to reference this common script in all other scripts, so all my scripts begin like this:

#!/bin/bash
source /path/to/bash-common.sh

In your case, your load.sh would then be:

#!/bin/bash
source /path/to/bash-common.sh

echo "Hello"
load_module virtuoso

You can then also source /path/to/bash-common.sh in your .bashrc if you want to.

In this way, you only have to maintain all your custom functions in one place, and they are accessible anywhere you source this file (including in your interactive shell if you source it in .bashrc).