
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.
Common Uses for the source Command
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:
source filename [arguments]
You can also use the shorthand dot (.
) notation:
. filename [arguments]
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.
Loading Configuration Files
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.
Command Syntax
source path/to/config_file
path/to/config_file
: Path to a file containing shell-compatible commands, such asexport
,alias
, or function definitions.
You can also use the shorthand version:
. path/to/config_file
Command Demonstration
Create a new configuration file named
.my_config
.console$ nano ~/.my_config
Add the following lines to define an environment variable and an alias:
iniexport MY_APP_PATH="/opt/myapp" alias ll='ls -la'
Save and close the file.
Source the configuration file to apply the changes.
console$ source ~/.my_config
Verify that the variable and alias are now available in the shell.
console$ echo $MY_APP_PATH && ll
Output:
/opt/myapp total 21 drwxr-xr-x 2 user user 4096 Dec 15 10:30 . drwxr-xr-x 18 user user 4096 Dec 15 10:25 .. -rw-r--r-- 1 user user 220 Dec 15 10:30 .my_config ...
Debugging with Existing Shell Variables
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.
Command Syntax
source script.sh
script.sh
: A script that references or modifies environment variables in the current shell session.
Command Demonstration
Set an environment variable in your current shell.
console$ export MY_VAR="Original value"
Create a script named
test.sh
.console$ nano test.sh
Add the following content:
bash#!/bin/bash echo "MY_VAR is: $MY_VAR" MY_VAR="Modified value" echo "MY_VAR is now: $MY_VAR"
Save and close the file.
Make the script executable.
console$ chmod +x test.sh
Run the script directly (in a subshell).
console$ ./test.sh
Output:
MY_VAR is: Original value MY_VAR is now: Modified value
Check the variable in your shell.
console$ echo "MY_VAR is: $MY_VAR"
Output:
MY_VAR is: Original value
The variable remains unchanged because the script ran in a subshell.
Source the script to execute it in your shell.
console$ source test.sh
Output:
MY_VAR is: Original value MY_VAR is now: Modified value
Check the variable again.
console$ echo "MY_VAR is: $MY_VAR"
Output:
MY_VAR is: Modified value
Loading Functions in Existing Scripts
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.
Command Syntax
source path/to/functions_file.sh
functions_file.sh
: A file that contains function definitions written in Bash syntax.
Command Demonstration
Create a function library file named
functions.sh
.console$ nano functions.sh
Add the following function definition:
bash#!/bin/bash system_info() { echo "System Information:" echo "===================" echo "Hostname: $(hostname)" echo "Uptime: $(uptime)" }
Save and close the file.
Source the file to load the function into your shell.
console$ source functions.sh
Call the function directly.
console$ system_info
Output:
System Information: =================== Hostname: ubuntu-server Uptime: up 2 hours, 34 minutes, 3 users, load averages: 4.20 4.41 3.87
Setting Environment Variables
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.
Command Syntax
source environment_file.sh
environment_file.sh
: A shell script that defines variables usingexport
.
Command Demonstration
Create an environment file named
app_env.sh
.console$ nano app_env.sh
Add the following variable definitions:
bash#!/bin/bash export DB_HOST="localhost" export DB_PORT="5432" export APP_ENV="development" export APP_PORT="8080"
Save and close the file.
Source the environment file to apply the variables.
console$ source app_env.sh
Create a script that uses the environment variables.
console$ nano test_env.sh
Add the following content to the file.
bash#!/bin/bash echo "Connecting to database at $DB_HOST:$DB_PORT" echo "Application running on port $APP_PORT" echo "Environment: $APP_ENV"
Save and close the file.
Give the execution permission to the file.
console$ chmod +x test_env.sh
Run the script to verify the environment variable usage.
console$ ./test_env.sh
Output:
Connecting to database at localhost:5432 Application running on port 8080 Environment: development
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
.
Conclusion
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.
No comments yet.