Results 1 to 8 of 8
Hi.
I tried it the way cabhan describet it, but there has to be a mitstake in my way.
I tried it like this:
main script:
Code:
#!/bin/bash
source functions.sh
...
- 06-14-2009 #1Just Joined!
- Join Date
- Jun 2009
- Location
- Germany
- Posts
- 3
How to include other functions in the other scripts
Hi.
I tried it the way cabhan describet it, but there has to be a mitstake in my way.
I tried it like this:
main script:
and the function (function.sh in the same folder)Code:#!/bin/bash source functions.sh echo "test: " _newecho exit 0
can anybody tell me what i am doing wrong?Code:#!/bin/bash function _newecho() { echo "Funktionstest" read testvar echo "$testvar" } exit 0
- 06-14-2009 #2Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
You need to use the $include directive.
Code:$include /path/to/functions.sh
- 06-15-2009 #3Just Joined!
- Join Date
- Jun 2009
- Location
- Germany
- Posts
- 3
Thanks, but that doesn't work either.
I didn't even know you could use $include, i thought in bash one had to use the dot to include. Well, i tried it, doesn't work
I also tried it like this:
doesn't work either. funny thing is, i don't even get an error message. it just like the script wouldn't to anythin, i dont eben get the echo "test: " to show in the terminal.Code:#!/bin/bash . ./functions.sh echo "test: " newecho exit 0
- 06-15-2009 #4Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
The dot (or source command) executes the script in the current shell environment. Since functions.sh only defines a bash function in it that's why nothing happened.
I tried the $include directive and it worked for me. What did the $include look like in your script?
- 06-15-2009 #5Linux Newbie
- Join Date
- Mar 2009
- Posts
- 228
I just figured it out. The 'exit 0' in functions.sh causes the script to terminate. Remove that from functions.sh and use the $include directive in your main script.
- 06-15-2009 #6Linux Guru
- Join Date
- Nov 2007
- Posts
- 1,695
No, source only "executes" when used on the command line.
But yes, once inserted, the exit call ends the script prematurely.Code:source, . (dot command) This command, when invoked from the command-line, executes a script. Within a script, a source file-name loads the file file-name. Sourcing a file (dot-command) imports code into the script, appending to the script (same effect as the #include directive in a C program). The net result is the same as if the "sourced" lines of code were physically present in the body of the script. This is useful in situations when multiple scripts use a common data file or function library.
Google "bash scripting guide" => Best Online Bash Guide
- 06-15-2009 #7Just Joined!
- Join Date
- Jun 2009
- Location
- Germany
- Posts
- 3
Thank you, guys. Now it works. Didn't think about the exit command. Now that I know it's pretty obvious.
I still got a question about the difference between source and the dot.
When using source its like the code would be physically in the script, the same variables are used etc. and if I use the dot to include it has it's own variables and i have to return them to the main script ?
- 06-15-2009 #8Just Joined!
- Join Date
- Jun 2009
- Posts
- 1
The dot (.) and source are bash builtins that are equivalent.


Reply With Quote
