What Are the Usages of Bash Source [3 Practical Examples]

In Bash scripting, sourcing a script allows you to run the commands within the script in the current shell session, rather than launching a new subshell. And this entire process is much more useful when you need to load environment variables, define functions, or execute commands that should affect the current shell session. In this article, I will explain the concept of sourcing in Bash with practical examples.

Key Takeaways

  • Learning the concept of Bash sourcing.
  • Seeing the process through practical examples.

Free Downloads

Difference Between Executing vs Sourcing a Bash Script

When you source or execute a script, the commands in the script are run line by line as if you typed them manually. Here are the key differences:

Executing a Script >

  • Opens a new shell, runs the commands in that shell, and returns the output to your current shell.
  • Changes made to the environment are limited to the new shell and are lost once it is closed.
  • Use it when you want to run the script independently without affecting your current shell.
  • Use the command syntax ‘./filename.sh’ to execute a file.

Sourcing a Script >

  • Runs the commands directly in your current shell.
  • Changes made to the environment take effect and persist in your current shell.
  • Use it when you want the script to modify the environment in your current shell.
  • Use the command syntax ‘. filename.sh’ or ‘source filename.sh’ to source a file.

3 Practical Examples of Bash Source

In this article, I will try to explain the concept of Bash sourcing with 3 practical examples. So that you can get a clearer overview of how sourcing in Bash works.

Example 1: Sourcing a Script Using the Bash “source” Command

In this example, I will simply source a script using the ‘source command‘. You can also use the ‘dot command‘ which is basically a shorter approach for the source command. In other words, a symbol (.) that represents the source command. Here, I will utilize the ‘env_setup.sh’ script that sets up some environment variables. Please go through the below steps to see the whole process:

Steps to Follow >

❶ At first, open your Ubuntu Terminal application.

❷ Now, let’s check out the contents of the script:

#! /bin/bash

export MY_VARIABLE=”Hello, World!”

EXPLANATION
  • #! /bin/bash: shebang or hashbang. It indicates the interpreter to be used for executing the script, in this case, it’s bash.
  • export: Makes the environment variables available to the child processes for the current shell session.
  • MY_VARIABLE: Variable name.
  • “Hello, World!”: Variable value.

➌ Now to source the ‘env_setup.sh’ script, write the following command in your command prompt & press ENTER:

source env.sh

Source a bash script file

NOTE: You won’t see any output on the terminal, but the environment variable ‘MY_VARIABLE’ is defined in the current shell session. And, you can access the variable value using the ‘echo command.

➍ Now, let’s access the value of “MY_VARIABLE” using the following command:

echo $MY_VARIABLE

Load a variableFrom the output image, you can see the MY_VARIABLE has been sourced to the current shell session successfully, as a result, it loads the value when called upon.

Example 2: Sourcing Functions Using the Bash “source” Command

Let’s consider two scripts, “Hello.sh” and “Olleh.sh”. In “Hello.sh”, you want to source and use a function that is defined in “Olleh.sh”. You can do that using the source command.  That way you don’t need to define the same function again for the second script. For that, let’s check out the contents of the two scripts:

Script (Olleh.sh) >

#! /bin/bash

#Defining function
hello_world () {
   echo “Hello, world!”
}

EXPLANATION
The script defines a function named ‘hello_world’ & the empty parentheses ‘{ }’ indicates that the function does not accept any arguments. After that, the opening & ending curly braces indicate the start & end of the function body with the echo command as the body.

Script (Hello.sh) >

#! /bin/bash

#Sourcing Olleh.sh using the source command
source Olleh.sh

#Calling the function defined in Olleh.sh
hello_world

EXPLANATION
The script uses the ‘source’ command to source the contents of the ‘Olleh.sh’ script. The script proceeds to call a function named ‘hello_world’ that is defined in ‘Olleh.sh’.

Now to source the defined function of the ‘Olleh.sh’ script to the ‘Hello.sh’ script‘ run the following command in your command prompt & press ENTER:

./Hello.sh

source a bash script The output of the Hello.sh script is the ‘defined function’ of Olleh.sh script.

Example 3: Modifying the Current Shell Environment

In this example, we will source a script that defines aliases. Aliases are shortcuts for longer commands or command combinations. Once sourced, Bash reads the contents of the script and applies these aliases as if they were directly defined in your current shell session, saving you from typing lengthy command combinations repeatedly.

Script (aliases.sh) >

#! /bin/bash

#Defining aliases
alias ll=”ls -l | grep Downloads”
alias new=”neofetch --ascii distro windows | lolcat”

EXPLANATION
The script defines aliases ( ll & new) for longer commands ‘ls -l | grep Downloads’ & ‘neofetch –ascii distro windows | lolcat’.

Now, write the following command to source the aliases written in the upper script:

source aliases.sh

Source defined bash aliasesTo check if the aliases are sourced successfully in the current shell session, just type the aliases & check the output like the following image:Load values of defined aliases From the output image, you can see, as the aliases are sourced successfully. So whenever I type them ( ‘ll’ & ‘new’ ) & simply press ENTER, as the output I get the output of the confined commands.

Conclusion

To sum up, Bash source is a powerful feature that allows you to execute commands, load variables, and import functions from files directly into your current shell session. In this article, I tried to clarify the concept of sourcing with some practical examples. Hope you find this helpful!

People Also Ask

What does source in Bash do?
When you use the ‘source’ command with a script, it effectively merges the contents of the script with the current shell environment, making any changes or definitions within the script immediately available in the current session.

What is Bash source file?
In Bash, a ‘source file’ refers to a script file that is read and executed within the current shell session using the ‘source’ command or its shorthand notation, the period ( . ) symbol.

Why use the source () command?
The source command in Bash is used for several reasons and offers specific advantages over executing a script as an independent process. Such as modifying the current shell environment, preserving variable assignments, loading functions & aliases, etc.

What is sourcing a shell script?
Sourcing a shell script means running the commands directly in your current shell. And changes made to the environment take effect and persist in your current shell. Source a script when you want the script to modify the environment in your current shell.

Related Articles


<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial

Rate this post
LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment