The Source command

source (contrary to the popular belief that this has to do with burgers!) is a way of getting commands to run inyour current shell (without opening a new shell, as would normally happen)

. shellscript

Notice there is a whitespace between the fullstop ( . ) and the shellscript script.

As we have covered before I can run this script with:


Then the following would happen:

  1. bash (your current shell) would call a script called shellscript

  2. shellscript would start a new shell (as directed by #!/bin/bash inside the script)

  3. the script would run and finally exit.

  4. control would be returned to your original bash shell

Figure 10.1. Parent- and sub-shells

Parent- and sub-shells

If on the other hand we sourced this script using the following command:

. ./shellscript

The following would happen:

  1. from your current shell, shellscript would be run. Notice, no starting a new shell.

  2. The script would run to completion.

  3. When the exit 0 is reached in shellscript, your current shell would vanish (if you ran this directly after logging in, you would be returned to the login prompt)

Firstly, why does the sourcing not begin a new shell? This is the point of sourcing.

The commands in shellscript are run from the current shell and the shebang at the beginning of the shell has no effect.

Secondly, why would you be returned to your login prompt?

On reaching the exit 0, the current shell exits, as it must. Since you were not running this script within a subshell, the current shell exits, leaving you at the login prompt.

One of the uses I have for sourcing I got from looking at the startup scripts in Linux. In these scripts, they keep a host of variables in files that can be modified by the user. At the time of running the script, they source these files, and bingo, they have variables set according to the users specifications.

I have included an example of this below:

Edit a file called vars and enter the following to set 4 variables:

COMPANY="QED Technologies CC"

You might want to set these variables up-front, before you start your script.

Edit a new script and at the start of your new script include the shebang, as normal, but also include the line '. var' as shown below:

. var		#sourcing the variables in the file var.
echo "$NAME $SURNAME was here"
exit 0

Now, to get the user to run this with different data in the NAME, SURNAME, etc. fields, they only need modify the 'var' file.

Other uses of sourcing include using it to define a set of reusable functions, then sourcing the script inside your shell script:


This can help immensely when writing large, complex shell scripts. More examples of using source will be given once we have discussed using functions.


  1. Change your prompt to read as follows:

    [ 21 March: 13:12pm : hamish@defender : /home/hamish/shell_scripting ] $
  2. .The prompt will naturally not have the exact date specified here, but the date and time will vary according to your system date. The user name will also vary according to who is currently logged in, and the path will vary depending on the path the user is working in at the time.

  3. Modify your script to obtain the default parameters from a file called restaurant.def. This file should contain two variables, namely RATING and TYPE. Ensure that on running the script, if no parameters are supplied, the default ratings are used. Note: you may have to alter your in more than one way, since in the last exercises, without any parameters ran in interactive mode.