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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

2 hours ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

3 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

8 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

9 hours ago

How To Use Pipes In Bash Scripts For Command Chaining

Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…

10 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

11 hours ago