Source Command in Linux

The source command is a built-in shell command used to read and execute commands from a file inside the current shell session. The source command is commonly used to retain/change the environment variable in the current shell. In short, sourcing a script will run execute commands in the current shell.

The source command is useful for:

  • Refreshing your current shell environment
  • To execute a shell script in the context of the current environment
  • To import a shell function in your script
  • Read variables from a shell script

Syntax for source command

The syntax for this builtin shell command is human readable. It takes a file and if arguments are provided they serve as positional parameters for the script being passed.


The . (dot) can also be used as an alternative for source command.


How to use source command

Here I explain some practical examples where you can apply source command.

1. Refresh your current shell environment

As a user you can define an alias in your current shell environment. To define one for ls -l type:

alias ll = 'ls -l'

To use it type:


Although the above list the files in the current directory in the long format, it works only for the current shell session. To make the changes permanently, open the ~/.bashrc file and add:

alias ll = 'ls -l'

To refresh the current shell environment type:

source ~/.bashrc

2. Execute a shell script in the context of the current shell environment

A shell script is not aware of the variables you define as a user in your current shell environment. The source command can be used to execute your shell script in the context of the current session.

To define a temporary variable type:


To create a custom script type:


Save the file. To execute it in the context of the current shell session type:

source ./

The output is shown below.

3. Import a shell function

To define a custom shell script type:

foo() {
 echo  "test"

Save the above as

To import the function of the above script in your current shell session, type:


To use the foo function type:


The output is shown below.


4. Read variables from a shell script

To create a shell script with some variables, type:


To read the variables within another shell script type:

echo $a, $b, $c

The output should be:

1, 2, 3

5. Read and execute commands

Source command can read and execute commands from a file. Lets have a text file with a set of commands.

For example file commands.txt has the following content:


The output of source <filename>:

$ source firstexample.txt
Fri Feb 25 11:10:11:09 GMT 2021

6. Pass arguments to functions

This section describes how to pass the parameter to the function and same function we can re-use via source command.
!/usr /bin/bash
 source 10 AA
 echo “var1 = $var1”
 echo “var2 = $var2”
var1 = 10
var2 = AA


Source command evaluvated script in the current shell whereas exec command runs in a new shell.

Through this article, you learned four practical examples of the source command. Although all of them are useful, the most important one for you as a user is the first one.

Leave a Comment