Reputation: 1
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
Upvotes: 4
Views: 5368
Reputation: 67831
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()
.
Upvotes: 10
Reputation: 1
If I source test1.sh it gives the desired result.
$ . test1.sh
bar
$ foo
bar
Upvotes: 0
Reputation: 70931
If you call source test2.sh
you will get the result you want.
If you want to be able to call foo whenever you start new terminal, place its definition in .bashrc or .profile file.
Upvotes: 3