[email protected]Office hours: Mon – Fri 9:00AM – 5:00PMLinkedInXFacebook
NexonHost
Netherlands Dedicated ServersAmsterdam · Equinix AM6Germany Dedicated ServersFrankfurt · Equinix FR4Romania Dedicated ServersBucharest · VoxilityAustria Dedicated ServersVienna · Interxion VIEBulgaria Dedicated ServersSofia · TelepointFrance Dedicated ServersParis · Interxion PAR — Marseille · Digital Realty MRS3Ireland Dedicated ServersDublin · Equinix DB2Italy Dedicated ServersMilan · Irideos AvalonPoland Dedicated ServersWarsawSpain Dedicated ServersMadrid · Equinix MD2United States Dedicated ServersAshburn · Equinix DC
All articles
Getting startedApril 15, 2023

How to use Bash Source Command.

How To Use Bash Source Command.

The source command reads and executes commands from the file specified as its argument in the current shell environment. It is useful to load functions, variables, and configuration files into shell scripts.

Source is a shell built-in in Bash and other popular shells used in Linux and UNIX operating systems. Its behavior may be slightly different from shell to shell.

 

Source Command Syntax

The syntax for the source command is as follows:

source FILENAME [ARGUMENTS]
. FILENAME [ARGUMENTS]
  • source and . (a period) are the same command.
  • If the FILENAME is not a full path to a file, the command will search for the file in the directories specified in the $PATH environmental variable . If the file is not found in the $PATH, the command will look for the file in the current directory.
  • If any ARGUMENTS are given, they will become positional parameters to the FILENAME.
  • If the FILENAME exists, the source command exit code is 0, otherwise, if the file is not found it will return 1.

 

Source Command Examples

In this section, we will look at some basic examples of how to use the source command.

 

Sourcing Functions

If you have shell scripts using the same functions, you can extract them in a separate file and then source that file in your scripts.

In this example, we will create a file that includes a bash function that checks whether the user running the script is the root, and if not, it shows a message and exits the script.

functions.sh
check_root () {
  if [[ $EUID -ne 0 ]]; then
    echo "This script must be run as root" 
    exit 1
  fi
}

Now in each script that needs to be run only by the root user, simply source the functions.sh file and call the function:

#!/usr/bin/env bash

source functions.sh
check_root

echo "I am root"

If you run the script above as a non-root user, it will print “This script must be run as root” and exit.

The advantage of this approach is that your scripts will be smaller and more readable, you can reuse the same function file whenever needed, and in case you need to modify a function you’ll edit only one file.

 

Bash Configuration file

With the source command, you can also read variables from a file. The variables must be set using the Bash syntax, VARIABLE=VALUE.

Let’s create a test configuration file:

config.sh
VAR1="foo"
VAR2="bar"

In your bash script, use the source command to read the configuration file:

#!/usr/bin/env bash

source config.sh

echo "VAR1 is $VAR1"
echo "VAR2 is $VAR2"

If you run the script the output will look like this:

VAR1 is foo
VAR2 is bar

 

Conclusion

In this guide, you have learned how to use the source built-in command in your shell scripts.

If you have any questions or feedback, feel free to leave a comment.

Keep reading
How to Implement Network Segmentation and Resource Isolation in Linux.
February 6, 2024

How to Implement Network Segmentation and Resource Isolation in Linux.

How to Implement Network Segmentation and Resource Isolation in Linux. Securing and managing networks in a Linux environment involves advanced practices like network segmentation and resource isolation. This guide explores these concepts, providing detailed instructions and examples for…

Read article
How to open or close ports in Linux.
February 6, 2024

How to open or close ports in Linux.

How to open or close ports in Linux. Effectively managing ports on your Linux system is crucial for both security and service accessibility. This comprehensive guide will provide a detailed walkthrough on opening and closing ports using the robust tools iptables and ufw. Understanding these…

Read article
How to use ypdomainname command.
January 23, 2024

How to use ypdomainname command.

How to use ypdomainname command. In the realm of Linux networking, the ypdomainname command plays a crucial role in the context of the Network Information Service (NIS). NIS, formerly known as Yellow Pages, is a distributed database service that facilitates the sharing of network configuration and…

Read article
Get in Touch

Together, Let’s Build a Faster, Safer Internet.

Build scalable infrastructure with NexonHost — high-performance dedicated servers, VPS hosting, colocation, and DDoS protection across Europe.

Get Started →Contact Us
[email protected]Office hours: Mon – Fri 9:00AM – 5:00PM