The source appears to be a built-in shell statement that reads and executes the contents of a document (commonly a packet of instructions) that is provided as a parameter within the current shell script. Getting the contents of the given files, the instruction delivers them to the TCL interpreter only as a text program, which would then be executed. Every time the file name is called, any given argument turns out to be positional arguments. Positioning variables, on the other hand, remain unchanged. The $PATH items have been used to locate the folder containing FILENAME; However, unless the document is not in $PATH, the root folder will be searched. The source command seems to have no options and, in fact, the document seems to be the only parameter. Let’s start working on the Linux source command on Ubuntu 20.04 Linux operating system. Log in from your system and launch the Ubuntu terminal using the shortcut Ctrl + Alt + T.
Create a text file named “test.txt”
by consuming the “touch” instruction below and press Enter to execute it.
Open the home directory of your Linux system via File Explorer and double-tap the newly created “test.txt” file. We write the code shown below 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 display the current directory of the file. Therefore, “pwd” will display the current directory of a file. Another echo statement has been used to show some data. The built-in date method has been called to display the current date. Save the file by pressing Ctrl+S.
There are two ways to run a file with the source command. One method is to provide the entire path of a file within the source command as shown below. It will run the file and display the current directory of a file where the current date and time resides.
Another method to run the files is by simply using a file name in the command. It will look for this file in the root directory of a system. The result would be the same as the previous one.
Let’s have another example to see how a source command works in the shell. First, create a new “new.txt” text file using the keyword “touch” within a command as shown below.
Navigate to the root directory of your system through the file explorer found on your desktop. Open the new file.txt by double-tapping on it. Now we have added the script shown below. The first line contains the keyword “ls” to display the list of all files and folders in the directory we are currently working on. Two other keywords have been used to indicate the date and time of the current time zone in which you are working. Save the code file and close it.
Open the terminal once again, run the source command and name a file “test.txt” as shown below. Do not forget that we have been using the source command to locate the file in the home or root directory of our system. When you run this file, you can see that it displays the list of files and folders that reside in the home directory and the current date and time. One thing to note is that the file we have been running using the source command has been highlighted in the output. Another point is that if the “new.txt” file is not located in the home directory, the query may not work well.
In the previous examples, we have used individual files to work out the operation of a source statement. This time we will use two files to see how the source command works on Linux. On the other hand, this time we will use the bash files instead of simple text files. You need to understand that we can use the script functions in another file while getting the supply. So, open the shell and create a first bash file named “main.sh” using touch as follows:
Let’s open the “main.sh” file from the root directory after tapping it twice. Write the code presented below into your file. We’ve added bash support to our archive first. After that, we have created a “func” function 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 root”. The loop ends and so does the function. Save and close the file.
When you run this bash file,
it will not display anything because the “func” function 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 following script. This file has been getting the file main.sh in it. After getting the main.sh file, call the “func” function implemented in the main.sh file. Another echo statement has been used to show “I am sudo/root user” outside the feature.
The execution of the file
test.sh using the bash command shows us the instruction of the “func” function of the main.sh file because we have logged in from a non-root user
Let’s log in as root using the “su” command and then add the password sudo. Re-enter the bash command to run the file. This time, the echo statement of the “test.sh” file has been executed in the output, as the if statement of the main.sh if file does not meet its requirements.
Let’s have another illustration. Open the main.sh file and write the following script with bash support. We have added two variables, “one” and “two”, with some values in both. Save the file one more time.
Now, open the test.sh, file and update your code as below. We have been getting the “main.sh” file on it. After that, we have been printing the declarations along with the variables declared in the main.sh file.
Run the “
test.sh” file using the bash command, and you can see that it displays the output instructions and variables declared in the main.sh file
This will be our last example for the source command. Update the main.sh file as follows. This time, we have used a new method, “show()”, to display the currently logged USER.
We have updated the test.sh file
by getting main.sh file in it and calling the function “show()”.
Running the file test.sh shows the current user “aqsayasin” as a result because we are currently working on it
We learned well how to use the source-built command within our command line in this article. Both bash and source command look the same while working, but apparently, they’re a little different. Both instructions may still have been used to execute a script.