Linux source command
1. What is the source command
source is a built-in shell command in Linux/Unix systems, used to execute the commands in the specified file within the current shell environment. Unlike directly running a script, the source command does not create a new subshell; instead, it directly executes the file contents in the current shell.
Analogy for Understanding
You can think of the source command as a "copy and paste" operation:
- Normal script execution: like sending the file to someone else to execute (opening a new terminal)
- source command: like directly pasting the file contents into your current terminal and executing them
2. Basic syntax of the source command
Example
Or use the equivalent dot (.) notation:
Example
Parameter Description
- filename: The path to the script file to execute (can be a relative path or an absolute path)
- parameters: Optional, parameters passed to the script
3. Difference between the source command and directly executing a script
| Comparison Item | source command | Directly executing a script |
|---|---|---|
| Execution environment | Current shell environment | New subshell environment |
| Variable impact | Changes the environment variables of the current shell | Does not affect the current shell's environment |
| Process relationship | Does not create a new process | Creates a new process |
| Exit impact | If the script contains exit, it will exit the current shell | Only exits the subshell |
4. Typical use cases of the source command
1. Loading environment variable configuration
The most common use case is loading~/.bashrcor/etc/profileconfiguration files such as:
Example
2. Activating a virtual environment
Activating a virtual environment in Python development:
Example
3. Updating the current shell environment
When you have modified a configuration file but do not want to log in again:
Example
4. Modular script development
Split a large script into multiple small files and use source to include them:
Example
source utils.sh # Include utility functions
source config.sh # Include configuration
5. Practical usage examples
Example 1: Loading custom environment variables
Create a test filemyenv.sh:
Example
export MY_VAR="Hello World"
alias ll='ls -alF'
Load using source:
Example
echo $MY_VAR # Output: Hello World
ll # Equivalent to ls -alF
Example 2: Debugging scripts
When debugging a script, you can use source to preserve the variables set in the script:
Example
# The variables set in the script can now be inspected in the current shell
6. Notes and common issues
- Security: source directly executes the file contents in the current environment; only source trusted files
- File lookup: source uses the current shell's PATH to find files; it is recommended to use a full path
- Error handling: If there are errors in the sourced file, the current shell will be affected
- Recursion issues: Avoid sourcing the file itself again within a sourced file, as this can cause an infinite loop
Common errors
Example
source ./script.sh # If script.sh is not readable
# Error: The file does not exist
source non_exist_file.sh
7. Comparison of alternatives
1. source vs export
exportOnly sets variablessourceCan execute arbitrary commands
2. source vs eval
evalExecutes a string rather than a filesourceSafer, with a clear file source
Linux command reference