Beginners Guide for Source Command in Linux

Linux TLDR
Last Updated:
Reading time: 4 minutes

The source is a built-in command-line tool that reads and executes the commands from the specified file (in order) as its argument in the current shell.

Each command from the specified file will be delivered to the TCL interpreter to be read and run, making it useful to load functions, variables, and configuration files.

Even when you modify the bash configuration (“~/.bashrc“) file, it is recommended to load the changes in the current shell environment using the source command due to its nature of running the script in the current shell environment instead of creating a new shell environment as the bash command does.

Tutorial Details

Difficulty LevelLow
Root or Sudo PrivilegesNo
Host System and ArchitectureUbuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Internet RequiredNo
Discussed Tools in this Article

Syntax of the Source Command

The source command takes two arguments: one is the filename (name or path of the file), and the other is the arguments (it will become the positional parameter for the file).

Note that the “.” (period) is a synonym for the source command, so both work the same way.


Also Read: How to Run a Shell Script in Linux

And do not confuse between “.” (period) and “./” (relative positioning), because both are different.

  • The “./” will run the script using bash by creating a new shell environment.
  • Whereas, “source” or “.” will read and execute the commands from the specified script or file into the current shell environment.

Evaluate the Contents of a Given File

Create a new text file with the name “commands.txt” in your current working directory and add the following commands to it.


Use the file path as a source command argument.

$ source commands.txt 
commands.txt  Documents  Music     Public  Templates
Desktop       Downloads  Pictures  snap    Videos

As you can see, the source command will read and execute the content of this file line by line in the current shell environment.

Specifying the Path of a Given File

In the previous example, the source command was able to read and execute the content of the file even without specifying the file’s absolute or relative path.

It is because if you do not specify them, the source command will search the “$PATH” environment variable for files. If it does not exist, source will search for the file in the current directory.

To demonstrate, I have renamed the previous file from “commands.txt” to “file” name (there is already a command with file name) as shown.

$ mv commands.txt file

Now, if you specify the “file” argument to the source command without specifying the absolute or relative path, it will search the “$PATH” variable for the specified file.

$ source file 
bash: source: /usr/bin/file: cannot execute binary file

As you can see above, there is a binary file with the name “file” in the “$PATH” variable that source found and tried to read and execute, but the file was a binary file, so source failed to read it.

To prevent this issue, you can specify the absolute or relative path for the specified file.

Example of an absolute path:

$ source /home/linuxtldr/file 
Desktop    Downloads  Music     Public  Templates
Documents  file       Pictures  snap    Videos

Example of a relative path:

$ source ./file 
Desktop    Downloads  Music     Public  Templates
Documents  file       Pictures  snap    Videos

Reading the Bash Configuration File

Using the source command, you can read variables from another file and load them into your existing file. To do that, you must declare the variables using the Bash “KEY=VALUE” syntax.

Create a text file in your current directory and copy the following content into it, then save the file with any name (ex: “env“).

VAR1="value one"
VAR2="value two"
VAR3="value three"

Now, create a bash script in the same directory with the name “” and add the following content.

#!/usr/bin/env bash

source ./env

echo "VAR1 is $VAR1"
echo "VAR2 is $VAR2"
echo "VAR3 is $VAR3"

If you run the “” file using the source command, it will read the “VAR1“, “VAR2“, and “VAR3” variables defined in the “env” file and execute the specified commands in your current shell environment as shown.

$ source 
VAR1 is value one
VAR2 is value two
VAR3 is value three

Sourcing Functions

If you have a block of commands or functions that you are using in multiple scripts, you can save them in a separate file and use the source command to refer to them when writing scripts.

For example, you can use the following “check_root” function to check whether the user running the script is a root user.

check_root() {
  if [[ $EUID -ne 0 ]]; then
    echo "You must run this script as root!"
    exit 1

Copy and save the above functions in the “” file.

Than create a second script with the name “” and simply use the source command to source the “” function.

#!/usr/bin/env bash

source ./

echo "I am root"

If you run the above script as a non-root user, it will print “You must run this script as root!” and exit.

Note: You can source the “” file into “” to load the function into the current shell environment. However, if you try to execute “” using the source command, your terminal window will close because source is not allowed to be run with sudo; instead, use the bash command.

$ bash 
You must run this script as root!

However, if you run your script as root or a sudo user, it will produce “I am root” and exit.

$ sudo bash 
I am root

The advantage of using this approach when creating scripts is that it will make the script smaller and more readable, reduce the repetition of codes, and make it easier to reflect changes in the file where the script is being used.

I hope this article makes it easier for you to understand the use of the source command.

If you have any questions regarding this topic, feel free to ask them in the comment section.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.