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()
share|improve this question
up vote 29 down vote accepted

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 source or . command and then simply call the function. See help source for more information.

share|improve this answer
    
The only problem with this method is that if you use exit in your function, it will close the terminal after the function executed. Is there any way around this? @SvenMarnach – user1527227 Sep 2 '14 at 23:01
    
@user1527227: Go with the next answer in that case, given you have control of the shell script. If you don't, you can call an interactive subshell first (just enter bash), and exit will only terminate the subshell, but not your terminal. – Sven Marnach Sep 3 '14 at 10:09

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 "$@" - which will expand to the arguments of the command line you specify, and since it's "alone" the shell will try to call them verbatim; and here you could specify the function name as the first argument. Example:

$ cat test.sh
testA() {
  echo "TEST A $1";
}

testB() {
  echo "TEST B $2";
}

"$@"


$ bash test.sh
$ bash test.sh testA
TEST A 
$ bash test.sh testA arg1 arg2
TEST A arg1
$ bash test.sh testB arg1 arg2
TEST B arg2

For polish, you can first verify that the command exists and is a function:

# Check if the function exists (bash specific)
if declare -f "$1" > /dev/null
then
  # call arguments verbatim
  "$@"
else
  # Show a helpful error
  echo "'$1' is not a known function name" >&2
  exit 1
fi
share|improve this answer
12  
Use "$@" in most cases. $@ is not safe in some cases. – fumiyas Apr 10 '15 at 13:21

The following command first registers the function in the context, then calls it:

. ./myScript.sh && func()
share|improve this answer

Briefly, no.

You can import all of the functions in the script into your environment with source (help source for details), which will then allow you to call them. This also has the effect of executing the script, so take care.

There is no way to call a function from a shell script as if it were a shared library.

share|improve this answer
    
I think this should be given more weight as an appropriate answer. I tried to do something similar to what the OP is wanting, but shell scripting simply isn't engineered for "clean-cut, OOP development" (IMHO). – Dan L Dec 18 '14 at 17:23

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":

function build() { 
  ... 
}

Then you can call it like this (from the directory where it is):

./control build

If it's inside another folder, that would make it:

another_folder/control build

If your file is called "control.sh", that would accordingly make the function callable like this:

./control.sh build
share|improve this answer

I have a situation where I need a function from bash script which must not be executed before (e.g. by source) and the problem with @$ is that myScript.sh is then run twice, it seems... So I've come up with the idea to get the function out with sed:

sed -n "/^func ()/,/^}/p" myScript.sh

And to execute it at the time I need it, I put it in a file and use source:

sed -n "/^func ()/,/^}/p" myScript.sh > func.sh; source func.sh; rm func.sh

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.