Say I have 2 scripts
test1.sh
#!/bin/sh
. ./test2.sh
foo
test2.sh
#!/bin/sh
foo(){
echo "bar"
}
If I call first script it is ok
$ ./test1.sh
bar
But if I try to call foo after that it will not work.
$ foo
bash: foo: command not found
When you execute ./test1.sh, a subprocess is spawned. When test1.sh sources test2.sh, only the context of that subprocess is modified when foo() is defined. As soon as test1.sh completes, the subprocess terminates and your interactive shell has no knowledge of foo().
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With