I have a script that has some functions.
Can I run one of the function directly from command line?
Something like this?
myScript.sh func()
Join Stack Overflow to learn, share knowledge, and build your career.
|
|
|
If the script only defines the functions and does nothing else, you can first execute the script within the context of the current shell using the |
|||||||||
|
|
Well, while the other answers are right - you can certainly do something else: if you have access to the bash script, you can modify it, and simply place at the end the special parameter
For polish, you can first verify that the command exists and is a function:
|
|||||
|
|
The following command first registers the function in the context, then calls it:
|
||||
|
|
|
Briefly, no. You can import all of the functions in the script into your environment with There is no way to call a function from a shell script as if it were a shared library. |
|||||
|
|
Edit: WARNING - seems this doesn't work in all cases, but works well on many public scripts. If you have a bash script called "control" and inside it you have a function called "build":
Then you can call it like this (from the directory where it is):
If it's inside another folder, that would make it:
If your file is called "control.sh", that would accordingly make the function callable like this:
|
||||
|
|
|
I have a situation where I need a function from bash script which must not be executed before (e.g. by
And to execute it at the time I need it, I put it in a file and use
|
||||
|
|