What's the difference between executing a script like this:
./test.sh
and executing a script like this:
. test.sh
?
I tried a simple, two-line script to see if I could find if there was a difference:
#!/bin/bash
ls
But both . test.sh
and ./test.sh
returned the same information.
Best Answer
./test.sh
runstest.sh
as a separate program. It may happen to be a bash script, if the filetest.sh
starts with#!/bin/bash
. But it could be something else altogether.. ./test.sh
executes the code of the filetest.sh
inside the running instance of bash. It works as if the content filetest.sh
had been included textually instead of the. ./test.sh
line. (Almost: there are a few details that differ, such as the value of$BASH_LINENO
, and the behavior of thereturn
builtin.)source ./test.sh
is identical to. ./test.sh
in bash (in other shells,source
may be slightly different or not exist altogether;.
for inclusion is in the POSIX standard).The most commonly visible difference between running a separate script with
./test.sh
and including a script with the.
builtin is that if thetest.sh
script sets some environment variables, with a separate process, only the environment of the child process is set, whereas with script inclusion, the environment of the sole shell process is set. If you add a linefoo=bar
intest.sh
andecho $foo
at the end of the calling script, you'll see the difference: