source Command in Bash: A Practical Guide

Last updated: December 27, 2022

Bash is a Unix shell and command language that provides users with an interface to the operating system. It is widely used on Linux and other Unix-like operating systems, as well as on Windows using the Windows Subsystem for Linux.

This tutorial will discuss about one of the most useful Bash command, source, which is used to execute commands from a file or script in the current shell environment.

What is source command in Linux?

The source command is a built-in shell command, which means that it is a command that is built into the Bash shell itself, rather than being a separate program that is invoked by the shell.

The source command is used to execute commands from a file or script in the current shell environment. It is similar to the "." (dot) command, which is also used to execute scripts. It is often used in the startup files of a shell, such as ".bashrc" or ".bash_profile", to set up the environment for the user.

Syntax

The syntax for the Linux source command is as follows:

source filename

The "filename" argument is the name of the shell script in current directory (or the directory containing filename) that you want to execute.

You can provide multiple filenames as positional parameters instead of typing the whole syntax again which makes terminal or shell scripts more readable.

How does it work?

When the source command is used, the Bash shell reads the specified file or script and executes the commands contained within it as if they had been typed directly into the shell. This allows users to define and execute commands and functions within a script.

For example, consider the following bash script named "myscript.sh":

#!/bin/bash

function greet() {
echo "Hello, $1!"
}

greet "earth"
sample script

If this script is executed using the source command, the output would be:

Hello, earth!
source myscript.sh

The function "greet" and the call to it are both executed in the current shell environment, so the function becomes available to be called later in the present shell session.

Why you may get source command not found?

One reason is that the source command is not built into the shell that you are using. For example, some older versions of the Korn shell and dash shell do not include the source command, and you may see this error if you are using one of these shells.

Another reason is that this bash command is not recognized as a built-in command in your Bash shell. This can happen if the Bash shell has been compiled without the source command, or if the source command has been disabled in the configuration.

In such cases, you can use the "." (dot) command instead of using source command in the same way.

Using with sudo

If you try to use the source with sudo as non root user, you may receive an error message saying that the command was not found. This is because the sudo command runs the script in a new shell with a different environment, so the changes made by the script are not reflected in the current shell.

Let's check how to use sudo with source (as root user).

You can read the script file with sudo and substitute it as pseudo-file into source as source filename to execute it into environment.

$ source <(sudo cat /etc/bash.bashrc)
source command permission denied

Or you can change current user to root user and execute the Linux source command with similar positional parameters in current directory.

$ sudo -s
# source filename

Common use cases for the source command

There are several common use cases for the source command:

  • Setting up the environment for a shell: It is often used in the startup files of a shell, such as .bashrc or .bash_profile in home directory, to set up the environment for the user.
  • Reloading configuration files: If you make changes to a configuration file, such as .bashrc in home directory, you can use the source command to reload the file and apply the changes to the shell environment.
  • Running a shell script and preserving the environment: If you want to run a shell script in current directory and have the changes it makes to the environment take effect immediately, you can use it to execute the script within the environment.

Examples

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

In summary, the Bash "source" command is a shell built in that allows you to execute a script within the current or working shell environment. It is often used to set up the environment for a shell, reload configuration files, and run a script and preserve the environment

Have you thought about the consequence if you accidentally run the source command on .bash_history file? Stay away from running it.

Navigate all-in-one place of Linux Commands for more learning.

About The Author

Bobbin Zachariah

Bobbin Zachariah

Bobbin started his career in IT in the year 2000 as a computer science instructor and worked as a Linux system engineer and senior analyst roles. Currently working in DevOps environments to increase efficiency and improve delivery time in AWS Cloud infrastructure. He is certified in RHEL, CCNA, and MCP and holds a Masters's in computer science. When in his free time love playing cricket, blogging, and listening to music.

SHARE

Comments

Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment