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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

15 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

16 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

16 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

16 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago