Source Command in Linux | Explained

Source command utilizes the same shell script and can be used to read the content of the file and then execute them. These files normally contain different commands, and this makes it very effective as you don’t need to type those commands every time whenever you execute them.

This article will provide you with detailed guidelines on how you can use a source command and what you can do with it. Two different methods will be discussed in this article which is mentioned below

Let’s discuss these two methods in detail below:

How to Use the Source Command in Linux?

Source command is used to read the executable code from any file and then later automatically execute it as well. To use this command, you need to follow the below-mentioned syntax:

$ source [filename]
How to install Kali Linux in Window...

Filename could be any file whose content you want to execute, and you can either execute a text or bash file, which we have explained in the next section.

How to Execute Text Files Using Source Command?

You can execute any text file using a source command, and for that, we will be creating a text file to process it via the source command. Let’s start!

Example 1: Perform update/upgrade Using the Source Command

Vim is a text editor we are using to create a text file you can also use other available ones as per your choice.

$ vim example1.txt

In the next stage, we will be writing some basic commands inside the text file, such as:

$ sudo apt update $ sudo apt upgrade

After that, we will save the file and then try to run the text file using a source command by typing the below-mentioned command in the terminal:

$ source example1.txt

Now, what this command will do is it will read the content of the file and then execute those commands that are written inside it.

Note: You can also run the same command by following the syntax below, giving you the same output:

$ . example1.txt

Example 2: Displaying the PWD, Date, and Time Using the Source Command

Now, let’s take another example by creating a text file again and executing different commands.

$ vim example2.txt

In this example, we are trying to execute the current directory (PWD: Present Working Directory) along with the date and time. So, we have written the following script inside the example2.txt file:

Now when you execute this text file using a source command, you will get the output as shown below:

$ source example2.txt

How to Run Bash Scripts Using a Source Command?

A source command can be used to run the bash scripts as well, so to give you an example, let’s first create the bash script by typing:

$ vim

This will create a bash file with the name of where ‘.sh’ is the bash script file extension. Now, the bash script will only execute when you first write the following lines inside the script:


Example 1: Executing pwd, ls, and cal Command using the Source Command

After that, you need to write any script (actually a set of Linux commands) that you would like to execute as you can see in the image below:

We have written three different commands inside a bash script, and now we will show you how you can run this script by typing:

$ source ./

This will give you an output that can be seen in the image below:

Example 2: Reading Variables from a Different Shell Script Using a Source Command

There is another variation where you can call one bash script from another one. For that, you need to create the bash file first by typing:

$ vim

As shown, we have declared some variables and assigned them some values.


Now, let’s create the second bash file where we will call the first file inside it:

$ vim

As you can see in the image below, we have mentioned the name of the “” file, which we want to call inside the “”:

echo "The value of first variable is" $var1
echo "The value of second variable is" $var2
echo "The value of third variable is" $var3

Later, it will display the information on the terminal when you use the source command as shown below.

Pro Tip: You can also use the source command to refresh the shell environment by typing the following command:

$ source ~/.bashrc

This is how you can use the source command in Linux.


The Source command can be used to execute scripts written in different file formats, such as text or bash, using the same shell script. This is very effective as you no longer need to execute multiple commands manually, and all you need to do is run these scripts. This post has briefly demonstrated the usage of the source command with practical examples.