2.7. Sourcing Scripts

In some circumstances, we might not want a script to be executed by a separate shell process.

For example, suppose we just made some changes to our .cshrc or .bashrc file that would affect PATH or some other important environment variable.

If we run the start up script by typing ~/.cshrc or ~/.bashrc, a new shell process will be started which will execute the commands in the script and then terminate. The shell you are using, which is the parent process, will be unaffected, since parent processes do not inherit environment from their children.

In order to make the "current" shell process run the commands in a script, we must source it. This is done using the internal shell command source in all shells except Bourne shell, which uses ".". Most Bourne-derived shells support both "." and "source".

Hence, to source .cshrc, we would run

shell-prompt: source ~/.cshrc
	

To source .bashrc, we would run

shell-prompt: source ~/.bashrc
	
or
. ~/.bashrc
	

To source .shrc from a basic Bourne shell, we would have to run

. ~/.shrc
	

2.7.1. Self-test

  1. What is sourcing?
  2. When would we want to source a script?