How To

Bash Source Command: How To Reload Scripts And Config Files

Introduction

The Bash source command is used to read and execute commands from a file in the current shell. It is commonly used to reload configuration files, load variables, import functions, and apply changes without opening a new terminal.

If you are learning Bash scripting, the source command is important because it helps you understand how scripts and shell environments work. It is often used with files like .bashrc, .profile, and custom configuration scripts. For Linux users, system administrators, and cybersecurity learners, source is useful for automation, environment setup, and tool configuration.

What Is The Source Command In Bash?

The source command runs a file inside the current shell session.

Basic syntax:

source filename

You can also use the dot . command, which works the same way:

. filename

Example:

source ~/.bashrc

This reloads the .bashrc file without closing and reopening the terminal.

Why Use Source Command?

Normally, when you run a Bash script like this:

./script.sh

It runs in a separate shell process. Any variables created inside that script may not be available in your current terminal after the script ends.

But when you use source, the script runs in the current shell. This means variables, functions, and settings can remain available after the file is executed.

Basic Source Command Example

Create a file:

nano config.sh

Add the following content:

website="kalilinuxtutorials.com"topic="Bash scripting"

Save the file. Now source it:

source config.sh

Print the variables:

echo $websiteecho $topic

Output:

kalilinuxtutorials.comBash scripting

The variables are now available in your current shell.

Source Command With Bash Functions

You can also use source to load functions.

Create a file:

nano functions.sh

Add this code:

show_info() {    echo "Current User: $(whoami)"    echo "Hostname: $(hostname)"    echo "Date: $(date)"}

Now source the file:

source functions.sh

Run the function:

show_info

This is useful when you want to reuse functions across multiple Bash scripts.

Reload .bashrc Using Source

After editing .bashrc, you can reload it using:

source ~/.bashrc

Example:

nano ~/.bashrc

Add an alias:

alias ll='ls -la'

Reload the file:

source ~/.bashrc

Now use the alias:

ll

Cybersecurity Example: Load Tool Variables

You can create a security tools configuration file:

nano security-config.sh

Add:

target="192.168.1.1"scan_dir="$HOME/security-scans"

Source it:

source security-config.sh

Use the variables:

echo "Target: $target"echo "Scan Directory: $scan_dir"

Conclusion

The Bash source command is useful for loading variables, functions, aliases, and configuration files into the current shell. It is commonly used to reload .bashrc, apply environment changes, and organize reusable Bash code.

For beginners, learning the source command helps you write cleaner and more flexible scripts. It is especially useful in Linux automation, cybersecurity scripting, environment setup, and command-line workflow management.

Cyber Defence

Recent Posts

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

9 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

DockerĀ is an open-source platform that lets you package and run applications inside containers. Each container…

19 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

20 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

20 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

20 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

21 hours ago