1

I have a utilitarian shell script full of goodies and wish to bring them to the bash environment. I used the command source before as analogous to import or include in other languages.

printer.sh

#!/bin/bash

printAwesome () {
    echo "Awesome!"
}

printMoo () {
    echo "Moo!"
}

I think it is only running the command source printer.sh on terminal. Is it really? Do we have any other procedure to follow?

In python, we have environments which we activate and deactivate.

4
  • 1
    Do you mean you'd like to have printMoo available as if you were using from printer import printMoo in Python? Dec 7, 2022 at 13:50
  • "I used the command source before..." vs. "I think it is only running the command source printer.sh on terminal." -- what or who is the "it" that is running something there in the second sentence? What do you mean with only running the command "on terminal"?
    – ilkkachu
    Dec 7, 2022 at 19:02
  • Can you edit to add an example of how you're trying to use those functions? Better yet, add a full printout of what you're doing, including the files involved and the error messages you get (along with what you expected to happen)
    – ilkkachu
    Dec 7, 2022 at 19:03
  • The answer marked as the answer satisfies me very much. Dec 8, 2022 at 0:02

1 Answer 1

1

If I understand correctly what it is you are trying to achieve, then loading your file with source is not enough. If you wish to use printer.sh as a module, you need to export each function's name that should be available outside your script. So printer.sh should look like this:

#!/bin/bash

printAwesome () {
    echo "Awesome!"
}

printMoo () {
    echo "Moo!"
}

export -f printAwesome
export -f printMoo

And then you can write something like this in myscript.sh:

#!/bin/bash

source printer.sh
echo "Let's run these functions ..."
printAwesome
printMoo
echo "Hey, that works now!"
6
  • 3
    There is no need to export the functions if you are just using them in the same shell that does the sourcing. There is also no need to keep the #!-line on the script that you source as this will just be treated as a comment.
    – Kusalananda
    Dec 7, 2022 at 13:58
  • I already created an alias on dotfile .bashrc, which may work for some punctual cases. In this case now, I wish to import in another context, like a bash session. Dec 7, 2022 at 14:24
  • When I try without the export stuff, my bash crashes. I will try with export -f Dec 7, 2022 at 14:37
  • @Kusalananda: I remember finding out about this trick exactly because omitting export didn't work. Can you explain which case would require it? Dec 7, 2022 at 14:42
  • 1
    @AnthonyLabarre if you define some function in script A and execute (not source) another script B from within A, then B can use functions that are exported by A. Exporting is not needed when B is sourcing A (like in the answer).
    – muru
    Dec 7, 2022 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .