Linux Commands

Linux Source Command with Examples

The source seems to be an integrated shell instruction that reads and executes the contents of a document (commonly a bundle of instructions) that is provided as a parameter within the present shell script. Upon obtaining the contents of the given files, the instruction delivers them to the TCL interpreter only as a textual program, which would then be run. Whenever filename is called, any arguments given turn out to be positional arguments. The positioning variables, on the other hand, stay unaltered. The elements in $PATH have been used to locate the folder that contains FILENAME; nevertheless, unless the document isn’t found in $PATH, it would be searched in the root folder. The source command seems to have no options, and indeed the document seems to be the only parameter. Let’s get started working on the Linux Source Command in Ubuntu 20.04 Linux operating system. Login from your system and launch the Ubuntu terminal by utilizing the shortcut Ctrl+Alt+T.

Example 01:

Create a text file named “test.txt” by consuming the “touch” instruction beneath and press Enter to execute it.

$ touch test.txt

Open the home directory of your Linux system via File Explorer, and double tap on the newly created file “test.txt.” We wrote the below-displayed code in it, having two echo statements to display some data in the terminal. After that, we have used the word “pwd,” which indicates the method to show the current directory of the file. Hence, “pwd” will show the current directory of a file. Another echo statement has been used to show some data. The date built-in method has been called to show the current date. Save your file by pressing Ctrl+S.

There are two ways to execute a file with the source command. One method is via providing the whole path of a file within the source command as below. It will execute the file and show the current directory of a file where it resides the current date and time.

$ source /home/aqsayasin/test.txt

Another method to execute the files is by just using the name of a file in the command. It will search this file in the root directory of a system. The output would be the same as above.

$ source test.txt

Example 02:

Let’s have another example to see the working of a source command in the shell. First, create a new text file “new.txt” by utilizing the “touch” keyword within a command as below.

$ touch new.txt

Navigate towards the root directory of your system via the file explorer held on your desktop. Please open the file new.txt by double-tapping on it. Now we have added the below-shown script. The first line contains the keyword “ls” to show the list of all files and folders in the directory we are currently working in. Another two keywords have been used to indicate the date and time of the current timezone you are working in. Save the code file and close it.

Open the terminal once again, execute the source command and the name of a file “test.txt” as below. Don’t forget that we have been using the source command to locate the file in our system’s home or root directory. When this file gets executed, you can see it shows the list of files and folders residing in the home directory and the current date and time. One thing to be noted is that the file we have been running using the source command has been highlighted in the output. Another point is that if the file “new.txt” is not located in the home directory, the query may not work fine.

$ source new.txt

Example 03:

In the above examples, we have used single files to elaborate the working of a source instruction. This time we will be using two files to see the functioning of the source command in Linux. On the other hand, we will be using the bash files instead of simple text files this time. You need to understand that we can use the script functions in another file while sourcing. Hence, open the shell and make a first bash file named “” using touch as follows:

$ touch

Let’s open the file “” from the root directory after double-tap on it. Write out the below-presented code in your file. We have added the bash support in our file first. After that, we have created a function “func” to add some instructions to it. We have added the if statement to check if the USER is root or not. If the user is not root, it will display “This code must be executed as a root.” The loop ends and the function as well. Save and close your file.

When you run this bash file, it will display nothing because the function “func” has not been called anywhere in the code.

$ bash

Let’s create another bash file, “”.

$ touch

Open the file from the home directory, and add the below script. This file has been sourcing the file in it. After sourcing the file, it calls the function “func” implemented in the file Another echo statement has been used to display “I am sudo/root user” outside the function.

Execution of the file using bash command shows us the statement from the function “func” of the file because we have been logged in from a non-root user.

$ bash

Let’s log in as a root user using the “su” command and then add the sudo password. Again enter the bash command to execute the file. This time, the echo statement from the “” file has been executed in the output as the if statement from file doesn’t meet its requirements.

$ su

$ bash

Example 04:

Let’s have another illustration. Open the file, and write the below script having the support of bash. We have added two variables, “one” and “two,” with some values in both. Save the file once again.

Now, open the, file, and update its code as below. We have been sourcing the “” file in it. After that, we have been printing the statements along with the variables declared in the file.

Execute the “” file using the bash command, and you can see it shows the output statements and the variables declared in the file.

$ bash

Example 05:

It will be our last example for source command. Update the file as below. This time, we have used a new method, “show(),” to display the USER currently logged in.

We have updated the file by sourcing file in it and calling the function “show().”

Execution of the file shows the current user “aqsayasin” as a result because we are currently working on it.

$ bash


We learned well how to utilize the source constructed command within our command line in this article. Both bash and source command looks the same while working, But apparently, they are a bit different. Both instructions may, even so, have been used to run a script.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.