Both source xxx.sh and ./xxx.sh are common methods for executing Shell scripts in Linux/Unix systems, and they have fundamental differences in execution environment, permission requirements, and their impact on the current Shell. The table below summarizes their core differences for easier understanding:
| Feature Comparison | <span>source xxx.sh</span> |
<span>./xxx.sh</span> |
|---|---|---|
| Execution Environment | Current Shell Process | New Sub Shell Process |
| Affects Current Shell | Yes (variables, functions, and environment changes are retained) | No (changes in the script disappear when the sub-process ends) |
| Permission Requirements | Only requires read permission | Requires executable permission (<span>chmod +x</span>) |
| Script Interpreter | Ignores Shebang line (e.g., <span>#!/bin/bash</span>) and always uses the current Shell |
Specified by the Shebang line in the first line of the script |
| Typical Use Cases | Loading configurations, refreshing environment variables, defining functions | Running standalone scripts or programs |
🧠 Core Difference: Execution Environment and Impact Scope
The main difference lies in whether the script runs in the current Shell process and whether its modifications affect the current Shell environment.
-
<span>source xxx.sh</span>(or its equivalent form<span>. xxx.sh</span>) directly reads and executes the commands in the script file within the current Shell environment. This means that variables, functions, and modifications to environment variables (like<span>PATH</span>) defined in the script will directly affect the current Shell session and will persist. A common use case is when you modify configuration files like<span>~/.bashrc</span>or<span>~/.bash_profile</span>, you can use<span>source ~/.bashrc</span>to apply changes immediately without needing to log back in. -
<span>./xxx.sh</span>starts a new sub Shell process (sub-process) to execute the script. All operations in the script, including variable settings and environment changes, are only valid within this sub-process. Once the script execution is complete, the sub-process ends, and these changes disappear, having no effect on the current Shell environment. This is commonly used to run standalone scripts or programs.
🔐 Different Permission Requirements
-
When using
<span>source</span>to execute a script, only read permission for the script file is required. -
When executing a script using
<span>./xxx.sh</span>, the script file must have executable permission. You can add executable permission using<span>chmod +x xxx.sh</span>. If the script does not have executable permission, attempting to run<span>./xxx.sh</span>will result in a “Permission denied” error.
📢 Specification of Script Interpreter
-
<span>./xxx.sh</span>relies on the Shebang in the first line of the script (e.g.,<span>#!/bin/bash</span>) to specify which interpreter to use for executing the script. The system will start the corresponding interpreter based on this indication. -
When using the
<span>source</span>command, the Shebang line in the script is ignored. The commands in the script will be interpreted and executed directly by your current Shell (which could be Bash, Zsh, etc.).
💡 How to Choose?
Decide which method to use based on your goals:
-
If you want the variables, functions, or environment settings in the script to take effect and persist in the current Shell (e.g., loading configuration files, defining function libraries), use
<span>source</span>. -
If you just want to run an independent script task without affecting the current Shell environment, then use
<span>./xxx.sh</span>(make sure the script has executable permission).