Bash

Source Multiple Files from Directory

admin by @admin ADMIN
1h ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
A common ~/.bashrc / ~/.zshrc pattern: split your config into ~/.bashrc.d/*.sh and source them all. Easier than one giant file.
Bash
Raw
# Source every .sh file in ~/.bashrc.d (alphabetical order)
if [[ -d "$HOME/.bashrc.d" ]]; then
    for f in "$HOME/.bashrc.d"/*.sh; do
        [[ -f "$f" ]] && source "$f"
    done
fi

# Same, but only readable files (defensive against broken permissions)
for f in "$HOME/.bashrc.d"/*.sh; do
    [[ -r "$f" ]] && . "$f"
done

# Source ALL the files but log which ones loaded (for debugging slow shell startup)
for f in "$HOME/.bashrc.d"/*.sh; do
    [[ -r "$f" ]] || continue
    t0=$EPOCHREALTIME
    . "$f"
    printf '%6.0f ms  %s\n' "$(echo "($EPOCHREALTIME - $t0) * 1000" | bc)" "$f"
done >&2
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.