What I know is when I am having two script files lets say a.sh
and b.sh
and to use the variable or function defined in script a.sh
, then . ./a.sh
works but ./a.sh
doesn't works. While running a shell script both ./script.sh
and . ./script.sh
works fine. What is the difference between running a script with ./script.sh
and . ./script.sh
?

- 969
- 1
- 11
- 23
2 Answers
. path/to/script
sources the file (executes it in the same shell). The other call forks a new shell process which executes the script.
Invoking a script in a child process will make its variables not available to the parent process. Sourcing a script will introduce and change variables in the same parent process.

- 246,190
- 53
- 318
- 364
-
Thanks knittl, I got it. Its basically creating a new shell that's why my functions defined in other file are unreachable. – neo Oct 22 '13 at 12:30
The notation . ./a.sh
is short for source ./a.sh
. source
is a built-in command of the executing shell to read the given file line-by-line and execute all that is written there as if it was typed in the shell directly. As a consequence, if there is an exit
statement in a.sh
, it will close the shell which sources this; typically, the xterm window closes then.
The notation ./a.sh
, however, starts a new process; this is done by the current shell fork
ing itself and then making the forked child exec
uting the given program. This is, in this case, a shell script again, so a new shell will be exec
uted. Everything this new shell does will not influence the original (parent) shell. If the child is not sent to the background, the parent then waits for the child to terminate.

- 56,346
- 20
- 107
- 159