-1

I use Ubuntu with Bash and I have a file hosting a script with 5 functions.

Sometimes I want to call some function of these functions (but not all of them), directly from the CLI.

I could say it's quite like executing a file with an alias, but in this case, I target a particular function within a file and not a file by itself.


What is the correct way to do so?

I thought of sourcing the file from my users "$HOME"/.bashrc but that would execute the entire file with all functions and I totally don't want that.

0

My approach of sourcing the file from "$HOME"/.bashrc is good, but I missed this:

I first started to program in vanilla JavaScript, and there, there is no such things as shell session, bashrc files, and sourcing in the current session (in a way that effect only this session or this and 1 or more others).

In Bash, what I should do is create the file with the functions but without calls, then source it (execute it in the current session). I shouldn't call them in the file because then all out them would be called when the file is sourced.

Given it is sourced from my users bashrc file, each time I move to another session, the file would be re-sourced for me, in that session as well, and I could call the functions regular there as well.


Thinking in a vanilla JavaScript paradigm on this Bash issue is what inhibited me from reaching the answer directly.

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.