• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
NameHero® Blog

NameHero® Blog

Web Hosting Tips & Resources From NameHero

  • AI Agents
    • OpenClaw
    • Claude Code
    • n8n
    • Hermes Agent
    • Open WebUI
    • Docker
  • Hosting
    • Web Hosting
    • WordPress Hosting
    • WooCommerce Hosting
    • Enterprise Hosting
    • Email Hosting
    • HeroicGuard
    • GPU Hosting
    • Website Builder
  • VPS
    • Managed VPS
    • Unmanaged VPS
    • Flex VPS
  • Reseller
  • Gaming
  • Domains
  • Account
  • Blog Home
  • Categories
  • Authors

How To Use the Bash Source Command And Why

Bhagwad Park

Published on: June 18, 2024

Categories: Linux Command Line, VPS Hosting, Website Development 0

The “source” command in bash is used to execute a script in the current shell instead of a separate one. This simple difference has a wide variety of implications as I’ll describe below. Regular users of Linux are most familiar with the source command because they use it to reload configuration changes in the bash environment, and this article explains how that happens.

Table of Contents
  • Equivalent to Running the Commands Directly on Bash
  • Running a Script Doesn’t Affect the Existing Bash Shell
  • “Source” Allows you to Run Scripts in Current Bash
  • Common Uses for the “Source” Command
    • Loading Configuration Files
    • Debugging with Existing Shell Variables
    • Loading Functions in Existing Scripts
    • Setting Environment Variables
  • Conclusion

Equivalent to Running the Commands Directly on Bash

The “source” command is used to execute scripts. The easiest way to understand it is that it’s equivalent to manually entering the commands one by one on the command line. You might think that’s true even while running scripts the traditional way, but you’d be mistaken.

For example, let’s say we type the following on the command line:

export VAR="value"

We can then access the “VAR” variable on the command line using echo, like this:

echo $VAR

Here’s a screenshot of the output:

Variable Has Been Exported
Variable Has Been Exported

As you can see, the “VAR” variable is now available in the current shell.

Running a Script Doesn’t Affect the Existing Bash Shell

Now let’s do the same operation above, but inside a script. Let’s say I create a script called “script.sh” that exports the variable. I’ll then run the script, and try and access the variable using bash. Here’s what happens.

Script Doesn't Set the Variable
Script Doesn’t Set the Variable

In the above screenshot, I carry out the following steps:

First, I create a script called “script.sh” using the “cat” command. In an earlier article, I showed you how to use the cat command for this. Next, I made the script executable – without this, the system will tell me that the command isn’t found when I try and run it. Next, I execute the script, and the execution proceeds without errors. However, when I tried to access the variable I set in the script using the echo command, it didn’t work as it did in the previous example. That’s because when I executed the script in a regular manner, it spawned a new subshell that created a separate process separate from the existing bash interface. The entire context of the new subshell was restricted to that subshell alone and didn’t spill over to the existing bash environment.

Had I tried to access the variable within the same script, it would have worked. But after execution, the script terminated, and with it, the new subshell containing the defined environment variable.

However, this doesn’t mean that you can’t output stuff to the existing bash shell using the “echo” statement from within a script. The standard output of the subshell spawned by the script is connected to the terminal output.

“Source” Allows you to Run Scripts in Current Bash

To show you the difference between “source” script execution, and how regular scripts work, I’ll run the same example as above, but this time I’ll use “source” to execute the script. Here’s what happens:

Executing the Script Using Source.png
Executing the Script Using Source.png

As you can see, this time, I use the following command to execute the script:

source ./script.sh

Note that I don’t need to run it using “sudo”. This is because “source” runs the script using the existing privileges that the user already has. If you need to access sudo privileges while running the script using source, you need to build the “sudo” command into the lines that make up the script.

Because I used the “source” command to execute the script, the variable that I set using the “export” statement applied to the existing bash shell. As a result, when the script ended, I was able to access it using an “echo” command.

The two examples above show you the basic difference between “source” and regular command execution.

Common Uses for the “Source” Command

Here are a few ways that Linux users can use the “source” command.

Loading Configuration Files

As you get more accustomed to Linux, you begin to have your preferences for how you want your environment to look. For example, my PuTTY shell has a terrible color scheme and when I list the contents of a directory, the folders have a dark blue color that’s very hard to distinguish from the black background of the terminal. So I like to convert the color of folders to something that contrasts better.

Others like to create aliases for common commands, or even have specific functions that perform a series of complicated tasks with just a simple command. These customizations are all located in a file called “.bashrc” or “.profile”, and when you make changes to these files, you need to reload them so that the customizations work in the existing session.

To do this, you can use the source command:

source ~/.bashrc

Running this command will allow you to apply the changes you make to .bashrc without starting a new shell session.

Debugging with Existing Shell Variables

As mentioned earlier, when you execute a script, it starts a new subshell, and all the variable changes you make in that script are restricted to that subshell. As a result, functions in that script don’t have access to your existing environment variables. This can make debugging functions hard, and even though there are workarounds like including the variables in the script, it’s easier if functions had access to your current environment.

This is where the “source” command comes in, as you can ask Linux to execute a script using your current working environment so you can see how it handles the variables in real-time. Using it is as simple as:

source test_script.sh

When you’re sure that the function works, you can then execute it as usual without “source”.

Loading Functions in Existing Scripts

The utility of the source command goes beyond merely using the current bash context. You can also use it to load existing functions into scripts. So if you have a file called “functions.sh”, containing useful functions that you want to use in an external script, you can load it like this:

source /path/to/functions.sh

Once included, you can call the functions from that file in your script. You can also execute the above command directly on the command line to give you access to the functions inside the file.

You can even take this a step further and initialize virtual environments like those you find in Python with commands like this:

source venv/bin/activate

This will activate the environment and set up the necessary environment variables.

Setting Environment Variables

One of the most common uses for the “source” command is to set environment variables for a specific project, separate from the ones used in .bashrc. Let’s say you’re working on a project requiring you to frequently access information like the database name, user, and password. Since the project might be temporary, or you may want to keep them separate, you don’t want to add these variables directly to .bashrc.

One solution is to create a separate file with the declarations and load them separately on the command line like this:

source env_vars.sh

You can run this before you start work on your project.

Conclusion

As you can see, the “source” command is useful for more than just changing color preferences on the command line. It’s an important tool for debugging and project management, and doubling as a function-importing tool in external scripts.

Bhagwad Park Profile Picture
Bhagwad Park

I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!

Related Posts

How to Use set -e -o pipefail in Bash, and Why

While writing scripts in bash, it's important to be careful about error handling. That's what set -e -o pipefail does.

Using Systemctl to Enable Services on Boot on Linux

You can use the systemctl tool to start services automatically on boot. We do this using the "enable" keyword.

Linux Process Management: ps, htop, kill, and nice

Linux has a sophisticated toolset to deal with processes in various circumstances from scripting, to monitoring, to control.

Linux find with exec: + vs ;

The "find" command is often used with the "exec" command. Here are the two options you can use, and the difference between them.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow & Subscribe

Exclusive promos, content and more!


Most Popular Posts

NameHero’s Recommended WordPress Plugin and Theme Setup

WordPress Hosting vs. Web Hosting – What’s The Difference?

How To Increase The InnoDB Buffer Pool Size

How To Fix A Stuck All-in-One WP Migration Import

How To Add A Subdomain In Cloudflare

Top Categories

  • WordPress
  • WordPress Tutorials
  • OpenClaw Hosting
  • Enterprise Hosting
  • WooCommerce
  • Web Hosting
  • Resellers
  • Website Security
  • Website Development
  • Website Performance
  • VPS Hosting
  • SEO Tips
  • Announcements
  • Domain Registration
NameHero

NameHero® proudly provides web hosting to over 40,000 customers with 99.9% uptime to over 750,000 websites.

  • Master Card
  • Visa
  • American Express
  • Discover
  • Paypal
Products
  • Web Hosting
  • Managed VPS Hosting
  • Unmanaged VPS Hosting
  • Flex VPS Hosting
  • WordPress Hosting
  • WooCommerce Hosting
  • Reseller Hosting
  • Enterprise Hosting
  • GPU Hosting
  • Email Hosting
  • HeroicGuard
  • Domains
  • Website Builder
  • AI Agent Hosting
Help & Support
  • NameHero Blog
  • NameHero Gaming Blog
  • Support
  • Help Center
  • Migrations
  • Affiliates
  • Gaming Affiliates
  • Call 1-855-984-6263
Company
  • About Us
  • Contact Sales
  • Reviews
  • Uptime
  • We're Hiring

Copyright © 2026 Name Hero, LLC. All rights reserved.
NameHero® is a registered trademark.

  • Privacy Policy
  • Terms of Use
  • Acceptable Use Policy
  • Payment Policy
  • DMCA