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.
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.
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.
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.
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.
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:
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.
When you run this bash file, it will display nothing because the function “func” has not been called anywhere in the code.
Let’s create another bash file, “test.sh”.
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.
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.
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.
$ bash test.sh
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.
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.
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.
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.
We have updated the file test.sh by sourcing main.sh file in it and calling the function “show().”
Execution of the file test.sh shows the current user “aqsayasin” as a result because we are currently working on it.
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.