source / . (Dot)
The source command (or its shorthand . dot) reads and executes another shell script in the current shell. Use it to reuse variable and function definitions from a separate file, or to reload a configuration file so changes take effect immediately.
Syntax
Load another script in the current shell (source command).
source file_path
The dot (.) command does the same thing (POSIX-compliant).
. file_path
Reload a config file (apply changes immediately).
source ~/.bashrc . ~/.bashrc
Load a function library.
source ./lib/functions.sh
Difference between source and bash
| Execution method | Description |
|---|---|
| source file | Runs in the current shell. Variables and functions defined in the file are available in the calling shell. |
| . file | Equivalent to source (POSIX-compliant syntax). |
| bash file | Runs in a subshell. Variables and functions are not inherited by the calling shell. |
| ./file | Runs in a subshell (same as bash — variables do not persist in the calling shell). |
Sample Code
Prepare the following two files. The file lib/config.sh defines shared settings.
lib/config.sh
#!/usr/bin/env bash
DB_HOST="localhost"
DB_NAME="myapp"
DB_USER="dbuser"
APP_ENV="production"
log() {
echo "[$(date +%Y-%m-%d\ %H:%M:%S)] $*"
}
Loading the config file with source makes its variables and functions available in the current shell.
main.sh
#!/usr/bin/env bash
source ./lib/config.sh
log "Connecting to database: $DB_HOST/$DB_NAME"
echo "Environment: $APP_ENV"
bash main.sh
[2026-03-06 12:00:00] Connecting to database: localhost/myapp
Environment: production
Here is a pattern for loading a .env file to set environment variables all at once.
.env
API_KEY=secret123
DEBUG=true
load_env.sh
source .env
echo "API key: $API_KEY"
bash load_env.sh
API key: secret123
source .env echo "API key: $API_KEY"
bash load_env.sh API key: secret123
This example shows the difference between source and bash. Because bash runs in a subshell, variables defined inside it do not carry over to the calling shell.
diff_test.sh
cat > /tmp/test.sh <<'EOF'
MY_VAR="hello from script"
EOF
# Run with bash (subshell: variable does not persist)
bash /tmp/test.sh
echo "After bash: ${MY_VAR:-undefined}"
bash diff_test.sh
After bash: undefined
When you load the file with source, it runs in the current shell, so the variable is inherited.
source /tmp/test.sh echo "After source: $MY_VAR" After source: hello from script
Notes
The key characteristic of source is that it runs in the current shell process. When you want changes to ~/.bashrc or ~/.zshrc to take effect immediately, use source ~/.bashrc.
Loading a "library file" that defines shared functions and variables with source is a common pattern for improving the maintainability of multiple scripts. If the target file does not exist, the command will fail, so it is safer to check for its existence first with [ -f file ] before loading.
[ -f config.sh ] && source config.sh
For details on conditional expressions, see test / [ ] / [[ ]].
If you find any errors or copyright issues, please contact us.