The source xxx.sh and ./xxx.sh commands are two common methods for executing Shell scripts in Linux/Unix systems. They have fundamental differences in execution environment, permission requirements, and their impact on the current Shell. The table below summarizes their core differences for quick understanding:
| Feature Comparison | source xxx.sh |
./xxx.sh |
|---|---|---|
| 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 read permission is needed | Executable permission required (chmod +x) |
| Script Interpreter | Ignores Shebang line (e.g., #!/bin/bash), always uses the current Shell |
Specified by the Shebang line in the script’s first line |
| Typical Use Cases | Load configurations, refresh environment variables, define functions | Run 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.
source xxx.sh(or its equivalent. xxx.sh) 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 (likePATH) defined in the script will directly affect the current Shell session and remain effective. A common use case is when you modify configuration files like~/.bashrcor~/.bash_profile, you can usesource ~/.bashrcto apply changes immediately without needing to log out../xxx.shstarts 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 executing a script with
source, only read permission for the script file is required. - When executing a script with
./xxx.sh, executable permission for the script file must be ensured. You can add executable permission using thechmod +x xxx.shcommand. If the script does not have executable permission, attempting./xxx.shwill result in a “Permission denied” error.
📢 Specification of Script Interpreter
- The
./xxx.shmethod relies on the first line Shebang (e.g.,#!/bin/bash) in the script file to specify which interpreter to use for executing the script. The system will start the corresponding interpreter based on this indication. - When using the
sourcecommand, 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
source. - If you just want to run a standalone script task without affecting the current Shell’s environment, then use
./xxx.sh(ensure the script has executable permission).