Bash Source Command
Introduction
Before we start talking about Bash source command, let's first understand-What is Source Command ?
The source
command in Bash is used to execute commands from a specified file within the current shell session. This command is particularly useful for running shell scripts or sourcing variables, functions, and aliases from external files.
In the current shell environment, the source command reads and executes commands from the file supplied as an input. Shell scripts can be used to load functions, variables, and configuration files.
In this tutorial, we will discuss Bash Source Command. We will also address a few FAQs on Bash Source Command.
Source Command Syntax
The source
command has the following syntax:
source FILENAME [ARGUMENTS]
. FILENAME [ARGUMENTS]
- The commands
source
and.
(a period) are the same. - If the
FILENAME
parameter is not a complete path to a file, the command will look for it in the directories listed in the$PATH
environment variable. If the file isn't in the$PATH
, the command will search the current directory for it. - If any
ARGUMENTS
are specified, they will be used asFILENAME
positional parameters. - The
source
command exit code is0
if theFILENAME
exists; otherwise, it returns1
if the file is not found.
Source Command Examples
We'll look at some simple examples of how to utilize the source
command in this section.
Sourcing Functions
If you have shell scripts that use the same routines, extract them into a separate file and use that file as a source in your scrips.
In this example, we'll make a file with a bash function that checks whether the user running the script is root and, if not, displays a notice before exiting the script.
check_root () {
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root"
exit 1
fi
}
Simply source the functions.sh
file and call the function: in any script that needs to be run solely by the root user.
#!/usr/bin/env bash
source functions.sh
check_root
echo "I am root"
If you run the script as a non-root user, it will quit with the message, "This script must be run as root."
The benefit of this technique is that your scripts will be smaller and easier to read, you'll be able to reuse the same function file many times, and you'll only have to edit one file if you need to change a function.
Bash Configuration file
You can also read variables from a file using the source
command. The variables must be set using the VARIABLE=VALUE
Bash syntax.
Create a test configuration file as follows:
VAR1="foo"
VAR2="bar"
To read the configuration file, use the source command in your bash script:
#!/usr/bin/env bash
source config.sh
echo "VAR1 is $VAR1"
echo "VAR2 is $VAR2"
If you run the script, the following is the result:
Output
VAR1 is foo
VAR2 is bar
FAQs on Bash Source Command
What is the purpose of the source
command in Bash?
The source
command allows execution of commands from a script or file within the current shell environment.
What is the difference between running a script with ./
and using the source
command?
When you run a script with ./
, it creates a sub-shell, and any environment changes (e.g., variable assignments) are limited to that sub-shell. In contrast, the source
command executes the script within the current shell session, making any environment changes accessible globally.
How do I use the source
command?
To use the source
command, type source filename
or . filename
, replacing filename
with the name of the script or file you want to execute.
Can I use absolute or relative paths with the source
command?
Yes, you can provide both absolute and relative paths to the source
command when specifying the file to run.
What is the shorthand notation for the source
command?
The shorthand notation for the source
command is .
, which can be used interchangeably. For example, both source filename
and . filename
achieve the same result.
Can the source
command be used with scripts written in languages other than Bash?
Yes, the source
command can be used with scripts written in languages other than Bash, as long as the script is compatible with the current shell environment.
Can I source functions, variables, and aliases from a file?
Yes, the source
command can be used to import functions, variables, and aliases from a file into the current shell session, making them accessible.
Conclusion
You learned how to use the source
built-in command in your shell scripts in this tutorial.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.