Open In App

source Command in Linux with Examples

If you’re new to the world of Linux, you might have heard about commands that do various tasks, but some like the ‘source’ command might seem a bit confusing at first. Don’t worry; let’s break it down step by step.

What is the Source Command?

The source command in Linux is like a magic wand that lets you bring in commands from a file and use them directly in your terminal. It’s like having a recipe book and being able to use the recipes right away without copying them down.

The ‘source’ command is a built-in feature of the shell, designed to execute commands stored within a file directly in the current shell environment. When you use the ‘source’ command, it reads the contents of the specified file, typically a sequence of commands, and executes them as if they were typed directly into the terminal. This process happens within the context of the current shell session, without spawning a new process or interpreter. If any arguments are provided with the ‘source’ command, they are passed as positional parameters to the commands within the sourced file. However, the positional parameters of the current shell session remain unchanged unless explicitly modified by the commands within the sourced file. The ‘source’ command looks for the specified file in directories listed in the $PATH environment variable. If the file is not found in any of these directories, it will search in the current directory. It’s important to note that the ‘source’ command does not offer any options; its only argument is the name of the file to be sourced

Syntax of Source Command in Linux

The syntax of the source command is straightforward:


Or its equivalent:

. filename

Here, ‘filename’ refers to the file containing the commands to be sourced.

Example of Source Command in Linux

Execution of Shell Scripts:

One of the primary use cases of the source command is to execute shell scripts within the current shell environment. This allows the script to manipulate variables, functions, and other shell features directly.

Imagine you have a shell script named ‘’ that sets a variable and then echoes its value:

# Set a variable

MY_VARIABLE=”Hello, from”

# Echo the variable


Now, let’s use the source command to execute this script within the current shell:


Executing a shell script


Modifying Environment Variables:

The source command is crucial for modifying the environment variables of the current shell session. This is especially useful when a script needs to export variables that affect the parent shell environment.

# Export a variable

export MY_ENV_VARIABLE=”This is my environment variable”

Now, let’s use the source command to modify the environment variables within the current shell:

Modifying Environment Variable


To verify the changes, you can echo the environment variable:



Loading Configuration Files:

Many applications in Linux rely on configuration files. Utilizing the source command, these configuration files can be loaded directly into the current shell, ensuring that changes take effect immediately.

Consider a scenario where you’ve made changes to your ‘.bashrc’ configuration file, such as adding a new alias:

# Add an alias to .bashrc

alias myalias=’ls -la’

Now, let’s use the source command to load the updated configuration file into the current shell:

source ~/.bashrc

Now, you can use the newly added alias ‘myalias’:


Loading configuration file


Defining Shell Functions:

Shell functions defined within a script can be made available in the current shell session by sourcing the script containing those functions.

Suppose you have a script named ‘’ that defines a shell function:

# Define a shell function

my_function() {
echo “This is my custom function”

Now, let’s use the source command to make this function available in the current shell:


Now, you can call the function directly within the shell:


Defining Shell Functions



In conclusion, the ‘source’ command in Linux serves as a powerful tool for seamlessly integrating commands, settings, and functions from external files directly into the current shell environment. Acting as a conduit between the terminal session and files containing instructions, the ‘source’ command executes the commands within the specified file as if they were typed directly into the terminal. This enables dynamic modification of environment variables, execution of shell scripts, loading of configuration files, and availability of custom shell functions within the current shell session. By harnessing the capabilities of the ‘source’ command, Linux users can enhance productivity and efficiency in system administration and development tasks, ushering in a new level of command-line prowess and flexibility.

Article Tags :