Linux Source Command with Examples

3

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.

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.

Linux Source Command with Examples Linux Source Command with Examples 1625613397 775 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613397 227 Linux Source Command with Examples

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.

Linux Source Command with Examples Linux Source Command with Examples 1625613398 862 Linux Source Command with Examples

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.

Linux Source Command with Examples Linux Source Command with Examples 1625613398 960 Linux Source Command with Examples

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.

Linux Source Command with Examples Linux Source Command with Examples 1625613398 370 Linux Source Command with Examples

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.

Linux Source Command with Examples Linux Source Command with Examples 1625613399 328 Linux Source Command with Examples

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 “main.sh” using touch as follows:

Linux Source Command with Examples Linux Source Command with Examples 1625613399 467 Linux Source Command with Examples

Let’s open the file “main.sh” 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.

Linux Source Command with Examples Linux Source Command with Examples 1625613400 696 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613400 693 Linux Source Command with Examples

Let’s create another bash file, “test.sh”.

Linux Source Command with Examples Linux Source Command with Examples 1625613400 732 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613401 889 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613401 218 Linux Source Command with Examples

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 “test.sh” file has been executed in the output as the if statement from main.sh file doesn’t meet its requirements.

Linux Source Command with Examples Linux Source Command with Examples 1625613401 901 Linux Source Command with Examples

Example 04:

Let’s have another illustration. Open the file main.sh, 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.

Linux Source Command with Examples Linux Source Command with Examples 1625613402 670 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613402 429 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613403 495 Linux Source Command with Examples

Example 05:

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

Linux Source Command with Examples Linux Source Command with Examples 1625613403 786 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613403 318 Linux Source Command with Examples

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

Linux Source Command with Examples Linux Source Command with Examples 1625613404 172 Linux Source Command with Examples

Conclusion:

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More