Connect with us

Website

How To Use the Bash Source Command And Why

Published

on

How To Use the Bash Source Command And Why

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.

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.

Advertisement
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.

Advertisement

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.

Advertisement

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”.

Advertisement

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.

Advertisement

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.

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.