Just:
for script in scripts/*.sh; do
. "$script"
done
The .
special sh builtin tells the shell to evaluate the code in the supplied file. csh has source
for a similar command, some shells including bash
support both .
and source
sometimes with slightly different semantics.
You'll also want to make sure you use .
/ source
on loadScripts.sh
. Doing ./loadScripts.sh
would start a new shell to interpret the script so define those functions in that short-lived shell, not in the invoking shell.
Also make sure a /
is included in the path passed to .
/ source
, for instance by running . ./loadScripts.sh
if in the current working directory, otherwise its path is or may be¹ looked up in $PATH
.
Doing find... | while read...; do . ...; done
is wrong, first because of all the reasons noted at Why is looping over find's output bad practice?, but also here because in several shells including bash
when the lastpipe
option is not enabled, the while
loop would be run in a subshell, so the functions in the sourced files would only be defined in that subshell.
If you wanted to use find
, but beware find
doesn't sort the list of files and includes hidden files, with bash
4.4 or above, you'd do:
while IFS= read -rd '' -u3 script; do
. "$script"
done 3< <(LC_ALL=C find scripts/ -mindepth 1 -maxdepth 1 -name '*.sh' -type f -print0)
Here adding a -type f
(to restrict to files of type regular) to justify the use of find
.
If using zsh
instead of bash
, you could just do:
for script (scripts/*(ND.)) . $script
As an equivalent (where .
restricts to regular files, and D
includes hidden ones).
Or:
for script (scripts/*(ND.)) emulate sh -c '. $script'
If those scripts are meant to be in sh
syntax as the .sh
extension suggests.
¹ bash's behaviour in that regard varies whether the posix
option is enabled or not.
loadScripts.sh
if I callisApi
I getisApi: command not found
. – Saeed Neamati Aug 07 '22 at 07:04loadScripts.sh
(with. /path/to/loadScripts.sh
or. ./loadScripts.sh
if in the current directory), if you intend those functions to be defined in the current shell. With just./loadScripts.sh
orbash ./loadScripts.sh
the functions are only defined in the new shell that is started to interpret that script. – Stéphane Chazelas Aug 07 '22 at 07:07loadScripts.sh
file and ran it./loadScripts.sh
. It does not have a function in it. It only contains your script. But I get `line 2: syntax error near unexpected token .' – Saeed Neamati Aug 07 '22 at 07:12do
. See edit. Again, note you need. ./loadScripts.sh
, not./loadScripts.sh
– Stéphane Chazelas Aug 07 '22 at 07:13