×

Search anything:

source command in Linux

Binary Tree book by OpenGenus

Open-Source Internship opportunity by OpenGenus for programmers. Apply now.

The source command reads and executes the contents of a file in the current shell environment. In this article, we learn about this command through various examples.

Table of contents.

  1. Introduction.
  2. Syntax.
  3. Command usage.
  4. Summary.
  5. References.

Introduction.

We use the source command in Linux to read and execute the contents of a file which we pass as an argument in the current shell environment. In other words, it reads a file and executes its contents as commands in the terminal.

Syntax.

We write source commands using the following syntax:

$ source FILENAME [ARGUMENTS]

We can also use a period instead of the word source:

$ . FILENAME [ARGUMENTS]

src1

We specify a file's full path in the FILENAME arguments, however, if the path is not a full path to the file source looks for it in the $PATH environment variable. If the file is also not found in the $PATH environment variable, source searches for it in the current directory.

IF we pass arguments, they are used as positional parameters to the FILENAME.
The source command returns an exit code of 0, that is if the file exists otherwise it returns 1.

Command usage.

Example 1.

As an example let's add some basic commands in a file, source1.txt.

cal
time ping -c 2 8.8.8.8
date

src3

Now let's run the source command and pass this file as its argument.

$ source source1.txt

src2

As we can see from the output, all the commands are executed and output displayed in the terminal.

Example 2.

We can also read variables from files using the source command. Let's create a file variables.sh that has two variables initialized:

VAR_A="Variable_A"
VAR_B="Variable_B"

To read the values of the variables, we create the following bash script - read_vars.sh:

#!/bin/bash

source variables.sh
echo "$VAR_A and $VAR_B"

To execute the bash script we write:

$ bash read_vars.sh

src4

Now when we execute the script, we see the variable initially declared in the file printed in the standard output.

Example 3.

While writing scripts, we sometimes create functions that are common to many scripts. For example, a function that checks if a user running the script is the root user or a script that checks if a user belongs to a certain group.

It is convenient to save these functions in different scripts then reference them when writing scripts using the source command.

For example, we have the following script that checks a user's group, if the user belongs to the group, then we can perform an action otherwise the action is not performed:

#!/bin/bash

checkGroup(){
    gidi="$(id -g $USER)"
    if [[ $gidi -ne 1001 ]]; then
        echo "User does not belong to group"
        exit 1
    fi
}

Here is the main script where we references to the above script to proceed. If the user indeed belongs to the group, we perform actions:

#!/bin/bash

source function.sh
checkGroup

echo "Done!!"

src5

Another example:
src6

This is applicable in cases where we want a certain group of users to be able to perform actions such as system updates or execute certain sensitive commands.

Example 4.

We also use the source command to refresh the current shell environment, for example, let's create an alias.

$ alias up='sudo apt-get update && sudo apt-get upgrade'

src7

After the command is executed. we will only be able to use the alias in the current terminal session.

When we open a new terminal session and try to execute the up command, it won't work since it was not permanent. To make it permanent we edit the ~/.bashrc and add the above line below the ls aliases.
src8

Still the alias will not work since it is not in the current shell environment. To use it we execute the command:

$ source ~/.bashrc

src9

Now the alias will work in all terminal sessions.

Summary.

We commonly use the source command to source environment variables into the current terminal session since any variable that is assigned a value in a script will retain its value after execution is complete.

Unlike the bash command which creates variables in a new shell, the source command creates variables in the same shell.

References.

For a comprehensive guide on how to use the source command in Linux. We can execute the command $ source --help in a Linux terminal.

source command in Linux
Share this