Several files: source
Keep shared functions in another file and load them with source.
Runs in Zerethon IDE · Bash 5.2
Official Bash documentation ↗
main.sh
source ./lib/strings.sh
shout "hello"
repeat ab 3
lib/strings.sh
shout() {
echo "${1^^}!"
}
repeat() {
local out="" i
for (( i = 0; i < $2; i++ )); do
out+="$1"
done
echo "$out"
}
Output
HELLO! ababab
main.sh (the entry) and lib/strings.sh with the functions.
source file(or. file) runs the file in the current script, so its functions and variables stay defined.- Paths are relative to the working folder, which is the project root.
- Running
bash other.shinstead starts a separate script; nothing it defines comes back.