×


An Overview of Source Command in Linux with Examples ?

Source is a command to read a file then execute its contents. It helps to load variables, functions, and configuration files into shell scripts.

Source is basically a shell built-in command and some shells in Linux and UNIX. It passed as an argument in the current shell script.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform related Linux system commands queries.

In this context, we shall look into how to use the source command in Linux.


What is the syntax of source command ?

The syntax of source command is given below:

$ source FILENAME [ARGUMENTS]

Here we can replace source command by the .(dot) to give:

$ . FILENAME [ARGUMENTS]


Different ways to use source command

1. Refresh current shell environment

When using Linux, a user can define alias in the current shell environment. For example, to display hidden files, we use ls -la. But we can use faster way by alias:

$ alias la='ls -la'

Then we only type la to display hidden file.

But this is only temporary, for permanent use, open the .bashrc file and type:

alias la= 'ls -la'

To Refresh the current shell environment, simply type:

$ source ~/.bashrc

2. Execute shell script in the current shell environment

A shell script can’t understand the variables you define in the current shell environment. So we must use the source command.

For example, we will try to run apt command.

Start by creating a file with name update.sh and start with:

#!bin/bash

Then, type the following command:

$ sudo apt update

Finally, save and execute it by source command:

$ source ./update.sh

3. Import a shell function

For example:

Start by creating a file foo.sh and start with:

#!bin/bash

Then, define a custom shell script. Here I put a function named foo:

foo () {
echo “Hello”
}

Now save the file.

To import the foo function, run the following command:

$ source foo.sh

To run the shell function, type:

$ foo

4. Read and execute commands

Let's say you want to execute 2 commands ls and df -h, let's create a file *txt contains those 2 commands. Here we will create file example.txt and save the following contents in it:

ls
df -h

And save it. Then run source filename:

$ source example.txt


[Need help in fixing Linux commands issues? We can help you. ]


Conclusion

This article covers how to use the source command in Linux. source reads and executes commands from file filename in the current shell. If filename does not contain a slash, directories in PATH are searched for filename.

Using source to execute the commands in a file is not the same as running a script. For one thing, the file does not need to be executable (e.g., with chmod u+x). For another, the commands will execute in the current shell environment; for example, any variables set will retain their value after the source is finished executing.


Source Command Syntax

The source command uses the following syntax:

$ source [filename] [arguments]

Where:

  • [filename]: The name or path to the file you want the source command to execute.
  • [arguments]: Any arguments you provide become positional parameters when the file is executed.