Score:0

Bash function defined in .bashrc not available to other scripts

vn flag

I execute this command frequently.

load_module virtuoso

It works fine in the terminal.

So I put it in a bash script named load.sh, which is executable.

    #!/bin/bash
    echo "Hello"
    load_module virtuoso

but when I try to execute it using ./load.sh, it gives this error,

./load.sh: line 3: load_module: command not found

I tried to debug it by putting echo "hello" in the 'load.sh' file. It prints hello on the screen when I execute the file, so the script works fine, I guess. I don't think about what else to try. Thanks

Here is the summary.

een212023@hertz:~/iec_lab1$ ls -la load.sh
-rwxr-xr-x 1 een212023 nogroup 46 Aug 19 17:59 load.sh
een212023@hertz:~/iec_lab1$ cat load.sh
#!/bin/bash
echo "Hello"
load_module virtuoso
een212023@hertz:~/iec_lab1$ load_module virtuoso
een212023@hertz:~/iec_lab1$ ./load.sh
Hello
./load.sh: line 3: load_module: command not found
een212023@hertz:~/iec_lab1$



een212023@hertz:~/iec_lab1$ type -a load_module
load_module is a function
load_module ()
{
    if already_loaded $1; then
        return 0;
    fi;
    load_vars_of_module $1;
    declare -a required_here=("${additional_required[@]}");
    declare -a conflicts_here=("${additional_conflicts[@]}");
    load_required_modules ${required_here[*]};
    unload_conflicting_modules ${conflicts_here[*]};
    load_vars_of_module $1;
    init_all_paths;
    add_paths ${additional_path[*]};
    add_ldpaths ${additional_ldpath[*]};
    add_manpaths ${additional_manpath[*]};
    add_lmpaths ${additional_lmpath[*]};
    exportvars ${additional_exports[*]};
    newaliases "${additional_aliases[@]}";
    unset ${!additional*};
    export MODULES=${MODULES}:$1
}
Byte Commander avatar
nf flag
What is `load_module`? Can you add the output of `type -a load_module` to your question please? If it is e.g. an alias, it will not be available in scripts.
Rahul avatar
vn flag
@ByteCommander I have added the output of "type -a load_module"
Byte Commander avatar
nf flag
Have a look at https://unix.stackexchange.com/q/63665/103151 - if you define a shell function e.g. in your .bashrc file, this will not be available to scripts, only to your interactive shell. You might convert the function into a separate script instead, or copy it into your new script, for example.
sourav c. avatar
cn flag
This is why `export` exists.
Rahul avatar
vn flag
@souravc. I have added `export -f load_module` in my `load.sh` file but upon executing is says, `load_module : not a function`
Byte Commander avatar
nf flag
You need to export the function where it is defined, not where you want to load it. But if I understand the post I linked correctly, depending on where you define it, that file might not be loaded for scripts anyway.
Score:1
vn flag

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).

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.