Bash source builtin command
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.
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.
source filename [argument ...]
You can also use a single dot (period), which is the same as the word source. For example:
. filename [argument ...]
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.
Read the file .bashrc in the home directory, and execute any commands in that file.
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 is a more reliable way to print escaped characters, such as \n, which is interpreted 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:
myvar="Hello" echo $myvar
These commands will 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 will need to specify the path name ./ (the current directory) as part of the command:
The script mysource is executed in its own shell environment, in which 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:
(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:
This time, however, myvar still has a value in the current environment:
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 will 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.