Source Command Basics
Understanding the Source Command in Linux
The source
command is a fundamental shell scripting tool in Linux systems, primarily used to read and execute commands from a file within the current shell environment. Unlike external script execution, source
allows direct interaction with the current shell context.
Core Functionality and Syntax
The basic syntax of the source
command is straightforward:
source filename
## or alternatively
. filename
Command Workflow
graph TD
A[Source Command] --> B[Read Script File]
B --> C[Execute Commands]
C --> D[Update Current Shell Environment]
Practical Use Cases
Scenario |
Description |
Example |
Environment Setup |
Load shell configuration |
source ~/.bashrc |
Variable Definition |
Import shell variables |
source config.sh |
Function Loading |
Reuse shell functions |
source functions.sh |
Code Example: Environment Configuration
#!/bin/bash
## config.sh
export DATABASE_HOST=localhost
export DATABASE_PORT=5432
export DEBUG_MODE=true
Executing source config.sh
will immediately set these environment variables in the current shell session, making them instantly accessible without spawning a new process.
The source
command executes scripts in the current shell context, avoiding process creation overhead and maintaining variable scope across script boundaries.