Linux source command

Linux 命令大全Linux command reference


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

sourcefilename[parameters]

Or use the equivalent dot (.) notation:

Example

. filename[parameters]

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

source ~/.bashrc

2. Activating a virtual environment

Activating a virtual environment in Python development:

Example

source venv/bin/activate

3. Updating the current shell environment

When you have modified a configuration file but do not want to log in again:

Example

source /etc/profile

4. Modular script development

Split a large script into multiple small files and use source to include them:

Example

# In the main script
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

#!/bin/bash
export MY_VAR="Hello World"
alias ll='ls -alF'

Load using source:

Example

source myenv.sh
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

source debug_script.sh
# The variables set in the script can now be inspected in the current shell

6. Notes and common issues

  1. Security: source directly executes the file contents in the current environment; only source trusted files
  2. File lookup: source uses the current shell's PATH to find files; it is recommended to use a full path
  3. Error handling: If there are errors in the sourced file, the current shell will be affected
  4. Recursion issues: Avoid sourcing the file itself again within a sourced file, as this can cause an infinite loop

Common errors

Example

# Error: Forgetting that the file needs to have executable permissions
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 variables
  • sourceCan execute arbitrary commands

2. source vs eval

  • evalExecutes a string rather than a file
  • sourceSafer, with a clear file source

Linux 命令大全Linux command reference

Other extensions