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
Description | Source |
Difficulty Level | Low |
Root or Sudo Privileges | No |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | source |
Internet Required | No |
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.
$ source [FILENAME] [ARGUMENTS]
$ . [FILENAME] [ARGUMENTS]
And do not confuse between “.
” (period) and “./
” (relative positioning), because both are different.
- The “
./script.sh
” will run the script using bash by creating a new shell environment. - Whereas, “
source script.sh
” or “. script.sh
” 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.
ls
whoami
pwd
Use the file path as a source command argument.
$ source commands.txt
Output:
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
Output:
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
Output:
Example of a relative path:
$ source ./file
Output:
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 “script.sh
” 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 “script.sh
” 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 script.sh
Output:
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
fi
}
Copy and save the above functions in the “check_root.sh
” file.
Than create a second script with the name “script.sh
” and simply use the source command to source the “check_root.sh
” function.
#!/usr/bin/env bash
source ./check_root.sh
check_root
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.
check_root.sh
” file into “script.sh
” to load the function into the current shell environment. However, if you try to execute “script.sh
” 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 script.sh
Output:
However, if you run your script as root or a sudo user, it will produce “I am root
” and exit.
$ sudo bash script.sh
Output:
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.