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 >
Sourcing a Script >
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!”
- #! /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
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
From 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!”
}
Script (Hello.sh) >
#! /bin/bash
#Sourcing Olleh.sh using the source command
source Olleh.sh
#Calling the function defined in Olleh.sh
hello_world
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
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”
Now, write the following command to source the aliases written in the upper script:
source aliases.sh
To check if the aliases are sourced successfully in the current shell session, just type the aliases & check the output like the following image: 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
Related Articles
- How to make a File Executable in Bash [2 Methods]
- A Complete Overview of Bash Dot Command [With 2 Examples]
- How to Run a Bash Script [2 Methods with Cases]
- How to Run Bash Commands in Parallel [4 Ways with Examples]
<< Go Back to Executing Bash Script | Bash Scripting Basics | Bash Scripting Tutorial
FUNDAMENTALS A Complete Guide for Beginners