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.

source FILENAME [ARGUMENTS]

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

. FILENAME [ARGUMENTS]

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:

ll

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:

WEBSITE = example.com

To create a custom script type:

#!/bin/bash
echo $WEBSITE

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

source ./myscript.sh

The output is shown below.

example.com

3. Import a shell function

To define a custom shell script type:

!#/bin/bash
foo() {
 echo  "test"
 }

Save the above as script.sh.

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

source script.sh

To use the foo function type:

foo

The output is shown below.

test

4. Read variables from a shell script

To create a shell script with some variables, type:

#!/bin/bash
a=1
b=2
c=3

To read the variables within another shell script type:

#!/bin/bash
source abovescript.sh
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:

pwd
date

The output of source <filename>:

$ source firstexample.txt
/home/developer
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.

functions.sh
!/usr /bin/bash
 var1=$1
 var2=$2
execute.sh
!/usr/bin/bash
 source functions.sh 10 AA
 echo “var1 = $var1”
 echo “var2 = $var2”
Output.sh
var1 = 10
var2 = AA

Conclusion

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