Bash source builtin command

Updated: 05/04/2019 by Computer Hope
source command

On Unix-like operating systems, source is a builtin command of the Bash shell. It takes a file name, and executes the commands in that file as if they had been typed on the command line.

Description

source reads and executes commands from file filename in the current shell. If filename does not contain a slash, directories in PATH are searched for filename.

When bash is not running in POSIX mode (e.g., set -o posix), the current directory is searched if filename is not found in $PATH.

If any arguments are supplied, they become the positional parameters when filename is executed.

Using source to execute the commands in a file is not the same as running a script. For one thing, the file does not need to be executable (e.g., with chmod u+x). For another, the commands will execute in the current shell environment; for example, any variables set will retain their value after the source is finished executing.

A common use of source is to load and execute the contents of a configuration file, such as $HOME/.bashrc. The .bashrc file is often executed automatically when you create a new bash shell. If you make changes to this file, and want to apply them to your current bash session, you can do so with source. See the examples below.

The builtin command . (a single period) performs the same action as the source command. The two can be used interchangeably.

Syntax

source filename [argument ...]

You can also use a single dot (period), which is the same as the word source. For example:

. filename [argument ...]

Exit status

The exit status of source is the exit status of the last command executed, or zero if no commands are executed. If filename is not found, or cannot be read, the return status is non-zero.

Examples

source ~/.bashrc

Read the file .bashrc in the home directory, and execute any commands in that file.

. ~/.bashrc

Same as the above command.

The next examples will illustrate how source executes in the current shell context:

printf 'myvar="Hello, World!"\necho $myvar\n' > ./mysource

Use the printf builtin command to print text, redirecting it into a new file named mysource. The printf command is used instead of echo because it's a more reliable way to print escaped characters, such as \n, which interprets as a newline. The entire string is enclosed in single quotes ('), rather than double-quotes, so that bash does not expand the exclamation point as a history expansion.

Verify the text of mysource using cat:

cat mysource
myvar="Hello"
echo $myvar

These commands set the variable myvar and then echo the value. Make mysource executable:

chmod u+x mysource

Then, run the script. Unless the current directory is in your PATH environment variable, you need to specify the path name ./ (the current directory) as part of the command:

./mysource
Hello, World!

The script mysource is executed in its own shell environment, where the variable myvar has a value ("Hello, World!"). After the script terminates, however, myvar no longer has a value. You can verify this by running echo $myvar at the bash prompt:

echo $myvar

(Only a newline is printed, because myvar has no value.)

Now, instead of running the script directly, execute it with source. The value of myvar is echoed, as before:

source ./mysource
Hello, World!

This time, however, myvar still has a value in the current environment:

echo $myvar
Hello, World!

As you can see, running a script creates an isolated shell environment, which is destroyed when the script terminates. In contrast, using source allows the contents of the script to affect the current environment. Being aware of the difference help you better understand the behavior of your scripts, and how they can affect the environment of other scripts and programs you run in bash.

exec — Destroy the current shell and replace it with a new process.