If you execute a script by setting the executable permission and then calling it by it's full or relative path, or just by name if it is in the PATH, then a new shell process will be created to interpret the script (the shell type determined by the shebang line at the start of the script).
If you source a script either by using the
source keyword or the
. syntax, i.e. one of these:
Code:
source test.sh
. test.sh
...then the file will be read and executed
in the current shell.
There are a few implications of using source over executing a script:
- Environment variables and other shell variables which are set during the course of the script's run will still be available after the script's contents have completed running.
- If you call exit from the sourced script, it will terminate the calling script / interactive shell.
- You won't incur the overhead of starting a new shell process. For one-off calls this isn't so interesting, but if you want to execute a script many times in a loop it might make a useful difference.