
The source command is a Bash shell builtin that executes commands from a file in your active shell environment, rather than launching a new subshell. Unlike running a script directly, source reads and evaluates the file's contents in your active shell session. As a result, any variables, functions, or environment changes take effect immediately.
This article covers common use cases for the source command, including loading configuration files, debugging scripts with existing shell variables, importing functions, and setting environment variables. You will learn the syntax and practical applications that make source an essential tool for Bash scripting and system administration.
Use the source command to execute a script in your active shell environment. This allows any environment changes or variable assignments to persist after the script runs.
The basic syntax is:
You can also use the shorthand dot (.) notation:
When you provide arguments, they become positional parameters ($1, $2, $3, and so on) inside the sourced script. This makes it possible to pass configuration values or options dynamically.
Use the source command to load configuration files that define environment variables, aliases, or other shell customizations. This approach is useful for making session-specific changes that take effect immediately.
path/to/config_file: Path to a file containing shell-compatible commands, such as export, alias, or function definitions.You can also use the shorthand version:
Create a new configuration file named .my_config.
Add the following lines to define an environment variable and an alias:
Save and close the file.
Source the configuration file to apply the changes.
Verify that the variable and alias are now available in the shell.
Output:
Use the source command to test scripts that rely on environment variables or need to modify the current shell state. Unlike direct execution, sourcing ensures that changes made inside the script persist in the calling shell.
script.sh: A script that references or modifies environment variables in the current shell session.Set an environment variable in your current shell.
Create a script named test.sh.
Add the following content:
Save and close the file.
Make the script executable.
Run the script directly (in a subshell).
Output:
Check the variable in your shell.
Output:
The variable remains unchanged because the script ran in a subshell.
Source the script to execute it in your shell.
Output:
Check the variable again.
Output:
Use the source command to load functions from an external file into your current shell session. This makes custom utilities reusable without rewriting code across multiple scripts.
functions_file.sh: A file that contains function definitions written in Bash syntax.Create a function library file named functions.sh.
Add the following function definition:
Save and close the file.
Source the file to load the function into your shell.
Call the function directly.
Output:
Use the source command to apply multiple environment variables from a single file. This method is useful for application configuration and development environments where consistent variables are required across sessions.
environment_file.sh: A shell script that defines variables using export.Create an environment file named app_env.sh.
Add the following variable definitions:
Save and close the file.
Source the environment file to apply the variables.
Create a script that uses the environment variables.
Add the following content to the file.
Save and close the file.
Give the execution permission to the file.
Run the script to verify the environment variable usage.
Output:
Environment variables set using source persist only in the active shell session where the command is run. To make them permanent, add the source command to your shell's startup file, such as .bashrc, .bash_profile, or .profile.
In this article, you used the source command to load configuration files, debug scripts with environment variables, import shell functions, and apply environment variables from external files. Unlike scripts that run in subshells, source applies changes immediately to your current shell session, making it a powerful tool for managing environment state and reusable configurations.
0 Comments
Be the first to comment and share your perspective with the community.