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.
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.
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.
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.
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.
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
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"
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.
Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…
Introduction A self-signed SSL certificate is a certificate that is created and signed by the…
Introduction Debugging is an important part of Bash scripting. When a script does not work…
Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…
Introduction Pipes are an important feature in Linux and Bash scripting. A pipe allows you…
Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…