Reputation: 5736
I have a function err()
in file abc
. The files do not have a .sh
extension, but they start with #!/bin/bash
.
err () {
echo "${1}" >&2
}
Now I am importing it in a different file xyz
:
source abc
someFunction(){
err "Failed to back up"
}
Is this the right way of importing?
Upvotes: 23
Views: 14444
Reputation: 535
You need to export newly created functions
at end of abc
add this:
export -f err
Upvotes: 3
Reputation: 44354
That's fine, here are some more hints:
Use a naming convention for functions, for example prefix the function name with f_
, for example f_err
. Function calls appear no different as other commands, this is a hint to the reader. It also reduces the chances of a name collision.
You need read access only, and you do not need the #!/bin/bash
(its just a comment).
In Bash, some options have to be set before function parsing. For example, shopt -s extglob
has to be done before and outside the function if it uses extended globbing. Putting that inside the function is too late.
Bash does not support the FPATH environment variable or autoload (as Korn shell does).
Upvotes: 5
Reputation: 48654
Yes, you can do like you mentioned above or like: . FILENAME
The file need not to end with .sh
Upvotes: 25