[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 610
  • Last Modified:

Bash script modular functions

Hello

I am trying to "include" fucntions from a files in my bash scripts similar to the way PHP does it.

For example if I have a file called functions.sh which includes commonly used functions, I want to include that in every bash script.

What I want to do is similar to php's include function. Is this possible?

Thanks for any and all help
jculkincys
0
jculkincys
Asked:
jculkincys
  • 2
1 Solution
 
xDamoxCommented:
Hi,

Simply test the following:

source functions_file

heres an full example:

test.sh
------------------
#!/bin/bash

source test2.sh

hello;

----------------

test2.sh
----------------
function hello() {
        echo "Hello World";
}
-----------------

you dont need to include the #!/bin/bash header in your fuctions because its already included
in your main file e.g. test.sh
0
 
jculkincysAuthor Commented:
I like it

Thank you kindly

P.S. are getting sick of answering my simple questions? haha
0
 
xDamoxCommented:
hehe Never :) I enjoy helping others
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now