FUNDAMENTALS A Complete Guide for Beginners
The source command in Linux executes commands from the file passed as an argument to it in the current shell environment. This command is beneficial for sourcing a script that sets variables, functions, or aliases in the current shell session.
A. Description
The source command serves the purpose of running the command from a file sequentially.
B. Syntax
The source command in Linux is a command that takes the filename and argument. The syntax for source commands is as follows.
source filename [argument]
C. Options
The source command in Linux is a shell built-in command. That’s why It doesn’t contain any man page. Moreover, there is not any option available for this command. To know more about this command, you can check its help page by executing the below command.
help source
Practical Examples of the “source” Command in Linux
The source command in Linux is a handy tool to execute commands from a file. The source command in Linux has many practical applications, and a few of them are outlined below.
Example 1: Executing Multiple Commands Line by Line from a File in the Current Directory
Using the source command, you can execute multiple commands line by line from a file. I have a file named command1.txt in my current directory, which contains the ls and date commands sequentially. I will use the source command to execute these commands from the command1.txt file. To do this task, follow the steps given below.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Then, copy the following command in the command prompt:
source command1.txt
➌ Now, press the ENTER button.
Output >
The source command executes the ls and date commands sequentially from the file command1.txt in the current directory, as illustrated in the following image.
- The “alias” Command in Linux [3 Practical Examples]
- The “unalias” Command in Linux [3 Practical Examples]
- The “exit” Command in Linux [5 Practical Examples]
Example 2: Executing Multiple Commands Line by Line from a File of a Different Directory Using the “source” Command in Linux
You can execute commands from a file in a different directory by specifying the path to that file. I have a file named command2.txt at ~/Desktop/ directory. In this example, I will be working with the command2.txt file which contains the following contents. Now, I will execute commands from this file using the source command. To do so, follow the below procedures.
Steps to Follow >
➊ At first, open the Ubuntu Terminal.
➋ Then, insert the following command in the command prompt.
source ~/Desktop/command2.txt
➌ Now, Hit the ENTER button.
Output >
The following image depicts that I have executed some desired commands from a file named command2.txt from a different directory with the source command.
Example 3: Executing Command Line by Line from a File in Bash Script Using the “source” Command in Linux
You can execute commands of a file inside a bash script. Here, I have a file named config.sh where the value of V1 and V2 are written. I will read the value of V1 and V2 from the sample_script.sh file using the source command. To achieve so, follow the below procedures.
Steps to Follow >
➊ At first, launch the Ubuntu Terminal.
➋ Then, type the commands in the command prompt as given below.
source sample_script.sh
➌ Now, tap the ENTER button.
Output >
The source command has read the value of V1 and V2 from the bash script named sample_script.sh, as shown in the following image. The value of V1 and V2 was originally stored in the config.sh file.
Conclusion
In this article, I have demonstrated some examples of the source command in Linux, which is a handy tool for executing multiple commands of a file sequentially. I hope you’ll be competent enough to explore more things with the help of these practical examples.
Related Articles
- The “bash” Command in Linux [5 Practical Examples]
- The “clear” Command in Linux [3 Practical Examples]
- The “declare” Command in Linux [7 Practical Examples]
- The “export” Command in Linux [8 Practical Examples]
- The “echo” Command in Linux [7 Practical Examples]
- The “hash” Command in Linux [6 Practical Examples]
- The “read” Command in Linux [8 Practical Examples]
- The “set” Command in Linux [6 Practical Examples]
- The “unset” Command in Linux [2 Practical Examples]
- The “eval” Command in Linux [4 Practical Examples]