[email protected]Support 24/7 · Billing 09:00 – 00:00LinkedInXFacebook
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 use dirs command in Linux.
October 12, 2023

How to use dirs command in Linux.

How To Use Dirs Command In Linux. In the world of command-line interfaces, efficient navigation and management of directories are essential skills. One such tool that can significantly streamline directory handling in Unix-based systems is the ‘dirs’ command. The ‘dirs’ command is often…

Read article
How to use dpkg command in linux.
October 12, 2023

How to use dpkg command in linux.

How To Use Dpkg Command In Linux. This guide explores the dpkg command syntax and options through various examples. dpkg in Linux is the primary package manager for Debian and Debian-based systems, such as Ubuntu. The tool installs, builds, removes, configures, and retrieves information for Debian…

Read article
How to use cksum command in Linux.
October 11, 2023

How to use cksum command in Linux.

How To Use Cksum Command In Linux. On Linux there are so many essential commands to perform various tasks or install the applications. One of the essential commands is cksum command which you can use to display a CRC (Cyclic Redundancy Check) value, the byte size of the file to standard output. In…

Read article
Get in Touch

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

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

Get Started →Contact Us
[email protected]Support 24/7 · Billing 09:00 – 00:00