Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux source command

Linux source command

Linux Command Reference Linux Command Reference


1. What is the source command

source is a built-in shell command in Linux/Unix systems used to execute commands from a specified file in the current shell environment. Unlike running a script directly, the source command does not create a new subshell; it executes the file's contents directly in the current shell.

An Analogy

You can think of the source command as a "copy and paste" operation:


2. Basic syntax of the source command

Example

source

File Name

[

Parameters

]

Or use the equivalent dot (.) notation:

Example

. filename

[

Parameters

]

Parameter Description


3. Difference between the source command and running a script directly

Comparison Item source command Run a script directly
Execution environment Current shell environment New subshell environment
Variable impact Changes the current shell's environment variables Does not affect the current shell's environment
Process relationship Does not create a new process Creates a new process
Exit behavior If the script contains exit, it will exit the current shell Only exits the subshell

4. Typical use cases of the source command

1. Load environment variable configuration

The most common use is to load ~/.bashrc or /etc/profile and other configuration files:

Example

source

~

/

.bashrc

2. Activate a virtual environment

Activating a virtual environment in Python development:

Example

source

venv

/

bin

/

activate

3. Update 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 smaller files and import them with source:

Example

In the main script

source

utils.sh  

import utility functions

source

config.sh

import configuration


5. Practical usage examples

Example 1: Load custom environment variables

Create the test file myenv.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: Debug a script

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. Safety: source executes the file's contents directly 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 the full path
  3. Error handling: if there are errors in the sourced file, they will affect the current shell
  4. Recursion issues: avoid sourcing the file itself from within a sourced file, as this will cause an infinite loop

Common mistakes

Example

Error: forgetting that the file needs executable permission

source

.

/

script.sh  

If script.sh is not readable

Error: file does not exist

source

non_exist_file.sh


7. Comparison of alternatives

1. source vs export

2. source vs eval

Linux Command Reference Linux Command Reference