The note is generated by “ChatGPT”.
🔹 source
- Executes a script in the current shell environment.
- Does not create a new shell process.
- All variable exports, alias definitions, and PATH modifications remain active after execution.
- Commonly used for reloading configuration files (e.g.,
source ~/.bashrc). - Bash and Zsh built-in command.
Example:
| |
→ Reloads your Bash configuration immediately in the current session.
🔹 bash
- Starts a new Bash subshell to run the script.
- The parent shell does not inherit any environment changes.
- Safer for running scripts that should not affect your current session.
- Suitable for installation, automation, or isolated execution.
Example:
| |
→ Runs the script, but changes (like export PATH=...) disappear when it exits.
🔹 .
- A POSIX-standard alias for
source. - Works in all POSIX-compliant shells (e.g.,
sh,dash,bash,zsh). - Functionally identical to
source.
Example:
| |
→ Same as source ~/.bashrc, but more portable across shells.
⚙️ Comparison Table
| Command | Runs in Current Shell | Creates Subshell | Persists Env Changes | Portability |
|---|---|---|---|---|
source script.sh | ✅ Yes | ❌ No | ✅ Yes | Bash/Zsh only |
. script.sh | ✅ Yes | ❌ No | ✅ Yes | POSIX (universal) |
bash script.sh | ❌ No | ✅ Yes | ❌ No | Bash only |
🧠 Quick Example
| |
Run it two ways:
| |
✅ Key Takeaway
- Use
bashwhen you want isolation. - Use
sourceor.when you want persistent changes in your shell session.