The “source” Command in Linux [3 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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]

Note: Here, the argument is enclosed by square brackets which refers that argument is not mandatory for the command.

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.command1.txt file contains ls and date command. 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 source command in Linux executes the ls and date commands sequentially from the file command1.txt in the current directory.



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.command2.txt contains some commands. 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.The source command executes the commands from the file command2.txt from a different directory.

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.The source command in Linux has read the value of V1 and V2 from the bash script named sample_script.sh. 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

5/5 - (1 vote)
Susmit Das Gupta

Hello everyone. I am Susmit Das Gupta, currently working as a Linux Content Developer Executive at SOFTEKO. I am a Mechanical Engineering graduate from Bangladesh University of Engineering and Technology. Besides my routine works, I find interest in going through new things, exploring new places, and capturing landscapes. Read Full Bio

Leave a Comment