Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call function from another shell script [duplicate]

Tags:

bash

shell

I have write a script, and I like to now to make it better readable, by moving parts of my main script in other files, but unfortunately I cannot.

Let's say now I have the following code in file utils.sh:

#!/bin/bash

sayHello ()
{
   echo "Hello World"
}

Them from my main script I try the following, but doesn't work:

#!/bin/bash

./utils.sh

sayHello

So, the question is, how to call the functions from within the utils.sh ?

like image 901
KodeFor.Me Avatar asked Nov 01 '25 08:11

KodeFor.Me


1 Answers

You have to source it, with . or source:

~$ cat >main.sh
#!/bin/bash
. ./utils.sh #or source ./utils.sh
sayHello

And then

~$ ./main.sh
Hello World
like image 198
fredtantini Avatar answered Nov 03 '25 00:11

fredtantini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!