source / . (dot)
| Since: | . (ドット) | POSIX(sh互換) |
|---|---|---|
| source | Bash(bash拡張) |
The source command (and its shorthand .) in shell scripting is a built-in command that reads and executes the specified file within the current shell process. Unlike running a script as a subprocess with bash script.sh, any variables, functions, and aliases defined in the script are applied directly to the calling shell. source is a bash/zsh extension, while . (dot) is the POSIX-compliant equivalent. The most common use case is immediately applying changes made to configuration files such as .bashrc or .bash_profile without restarting the shell.
source and . compared
source and . behave identically. You can use either one, but in POSIX-compliant scripts (#!/bin/sh), source may not be available, so use . instead.
| Syntax | Supported shells | Description |
|---|---|---|
source file | bash, zsh | Executes the file in the current shell. This is a bash/zsh extension. |
. file | sh, bash, zsh (all) | Identical behavior to source. This is the POSIX-compliant form and works in sh as well. |
# ----------------------------------------------- # source and . behave identically # ----------------------------------------------- # In bash / zsh, you can use source source ~/.bashrc # POSIX-compliant form that works in all shells including sh . ~/.bashrc # Both apply the configuration to the current shell (no subshell is created)
Difference from a subshell
Running a script with bash script.sh or ./script.sh creates a subshell (child process). Variables defined in a child process do not propagate back to the parent shell. Because source / . runs the script in the current shell without creating a subshell, any variables and functions defined in the script remain available afterward.
| Execution method | Process | Variables / functions inherited | Description |
|---|---|---|---|
bash script.sh | Subshell (child process) | Not inherited | Variables are gone when the script exits. |
./script.sh | Subshell (child process) | Not inherited | Requires execute permission. Variables are gone when the script exits. |
source script.sh | Current shell (same process) | Inherited | Variables, functions, and aliases persist after the script exits. |
. script.sh | Current shell (same process) | Inherited | Identical behavior to source. |
# -----------------------------------------------
# Comparing subshell execution vs. source
# -----------------------------------------------
# --- Running in a subshell ---
# Suppose you have a script that only sets the INSPECTOR variable
# (contents of set_inspector.sh: INSPECTOR="Kougami Shinya")
bash set_inspector.sh
echo "INSPECTOR: ${INSPECTOR}"
# ↑ Empty — variables set in a subshell do not return to the parent shell
# --- Running with source ---
source set_inspector.sh
echo "INSPECTOR: ${INSPECTOR}"
# ↑ Prints "Kougami Shinya" — the variable persists because the script ran in the current shell
Common patterns for loading configuration files
The most common use of source is splitting configuration into separate files and loading them from a main script. Extracting environment variables and shared functions into dedicated files improves the maintainability of your scripts.
| Pattern | Description |
|---|---|
| Reload shell configuration | Run source ~/.bashrc to apply changes to .bashrc without restarting the shell. |
| Load an environment variable file | Use source ./config.sh to keep environment variables in a separate file. |
| Load a shared function library | Use source ./lib/functions.sh to share function definitions across scripts. |
| Load only if the file exists | Use [ -f ./local.sh ] && source ./local.sh to safely load optional configuration. |
# -----------------------------------------------
# Safe pattern: source only if the file exists
# -----------------------------------------------
# Check for the file with -f before sourcing it
if [ -f "./local_config.sh" ]; then
source "./local_config.sh"
fi
# One-liner version using &&
[ -f "./local_config.sh" ] && source "./local_config.sh"
# -----------------------------------------------
# POSIX-compliant version (works in sh as well)
# -----------------------------------------------
[ -f "./local_config.sh" ] && . "./local_config.sh"
Sample code
psychopass_config.sh
#!/bin/bash
# -----------------------------------------------
# PSYCHO-PASS configuration file
# This file is intended to be loaded with source
# Running it directly has no visible effect
# -----------------------------------------------
# -----------------------------------------------
# Environment variables: system settings
# -----------------------------------------------
SIBYL_VERSION="3.1.0"
THREAT_THRESHOLD=100
LETHAL_THRESHOLD=300
# -----------------------------------------------
# Inspector and enforcer data defined as arrays
# -----------------------------------------------
INSPECTORS=("Kougami Shinya" "Ginoza Nobuchika" "Tsunemori Akane" "Masaoka Tomomi" "Shimotsuki Mika")
ENFORCERS=("Makishima Shogo" "Kagari Shusei" "Aoyanagi Risa" "Sugou Teppei" "Kasei Joshu")
# -----------------------------------------------
# Shared function: evaluates a crime coefficient
# and returns the corresponding hue rank
# Argument: $1 = crime coefficient (integer)
# Output: prints the hue rank string
# -----------------------------------------------
get_hue_rank() {
local coefficient="$1"
if [ "${coefficient}" -lt "${THREAT_THRESHOLD}" ]; then
echo "Clear (normal range)"
elif [ "${coefficient}" -lt "${LETHAL_THRESHOLD}" ]; then
echo "Yellow (under observation)"
else
echo "Dominator activation level (enforceable)"
fi
}
# -----------------------------------------------
# Shared function: returns the Dominator mode
# Argument: $1 = crime coefficient (integer)
# Output: prints the mode name
# -----------------------------------------------
get_dominator_mode() {
local coefficient="$1"
if [ "${coefficient}" -lt "${THREAT_THRESHOLD}" ]; then
echo "Safety lock"
elif [ "${coefficient}" -lt "${LETHAL_THRESHOLD}" ]; then
echo "Paralyzer mode"
else
echo "Destroy decomposer mode"
fi
}
psychopass_main.sh
#!/bin/bash
# -----------------------------------------------
# PSYCHO-PASS system: main script
# Loads psychopass_config.sh with source and
# uses the variables and functions it defines
# -----------------------------------------------
# -----------------------------------------------
# Load the configuration file with source
# → variables and functions are expanded into
# the current shell
# -----------------------------------------------
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
source "${SCRIPT_DIR}/psychopass_config.sh"
# -----------------------------------------------
# Confirm the configuration file was loaded
# -----------------------------------------------
echo "=== Sibyl System v${SIBYL_VERSION} booting ==="
echo " Threat threshold: ${THREAT_THRESHOLD}"
echo " Lethal threshold: ${LETHAL_THRESHOLD}"
echo ""
# -----------------------------------------------
# Display the list of registered inspectors
# (array variable was loaded via source)
# -----------------------------------------------
echo "--- Registered inspectors ---"
for inspector in "${INSPECTORS[@]}"; do
echo " ${inspector}"
done
echo ""
# -----------------------------------------------
# Crime coefficient check
# (shared functions were loaded via source)
# -----------------------------------------------
echo "--- Crime coefficient scan ---"
declare -a suspects=("Kougami Shinya" "Ginoza Nobuchika" "Tsunemori Akane" "Masaoka Tomomi" "Shimotsuki Mika")
declare -a coefficients=(280 88 42 195 130)
for ((i=0; i < ${#suspects[@]}; i++)); do
name="${suspects[$i]}"
coeff="${coefficients[$i]}"
# Call the functions loaded via source
hue=$(get_hue_rank "${coeff}")
mode=$(get_dominator_mode "${coeff}")
printf " %-20s coeff: %3d hue: %s → %s\n" \
"${name}" "${coeff}" "${hue}" "${mode}"
done
$ chmod +x psychopass_main.sh $ ./psychopass_main.sh === Sibyl System v3.1.0 booting === Threat threshold: 100 Lethal threshold: 300 --- Registered inspectors --- Kougami Shinya Ginoza Nobuchika Tsunemori Akane Masaoka Tomomi Shimotsuki Mika --- Crime coefficient scan --- Kougami Shinya coeff: 280 hue: Yellow (under observation) → Paralyzer mode Ginoza Nobuchika coeff: 88 hue: Clear (normal range) → Safety lock Tsunemori Akane coeff: 42 hue: Clear (normal range) → Safety lock Masaoka Tomomi coeff: 195 hue: Yellow (under observation) → Paralyzer mode Shimotsuki Mika coeff: 130 hue: Yellow (under observation) → Paralyzer mode
Reloading .bashrc (typical terminal usage)
# ----------------------------------------------- # Apply .bashrc changes without restarting the shell # ----------------------------------------------- # Edit .bashrc (for example, add an alias or environment variable) # vim ~/.bashrc # Apply the changes to the current shell with source source ~/.bashrc # . produces the same result (POSIX-compliant form) . ~/.bashrc # ----------------------------------------------- # Reload .bash_profile (login shell settings) # ----------------------------------------------- source ~/.bash_profile # For zsh, target .zshrc instead source ~/.zshrc
# Example: after adding an alias to .bashrc
$ echo "alias ll='ls -la'" >> ~/.bashrc
$ ll
bash: ll: command not found ← not yet applied
$ source ~/.bashrc
$ ll
total 48
drwxr-xr-x 6 user user 4096 Mar 27 10:00 .
drwxr-xr-x 20 user user 4096 Mar 27 09:00 ..
-rw-r--r-- 1 user user 220 Mar 27 09:00 .bash_logout
-rw-r--r-- 1 user user 3526 Mar 27 10:00 .bashrc
← after source, ll is now available
Summary
The source command (or .) in shell scripting is a built-in command that executes the specified file within the current shell process. Unlike running a subshell with bash script.sh, all variables, functions, and aliases defined in the script are inherited by the calling shell. source is available in bash and zsh, while . is the POSIX-compliant equivalent that also works in sh. Common use cases include reloading shell configuration files such as .bashrc, and splitting environment variables and shared function libraries into separate files. To avoid errors when the target file may not exist, use the pattern [ -f file ] && source file. For shell variable scoping, see Variables. For environment variables and export, see Special Variables.
If you find any errors or copyright issues, please contact us.