Linux source Command

The Linux source command is an essential tool for Linux users and administrators. It executes a script file in the current shell environment, allowing you to modify the current shell environment the same way you would if you had typed the commands manually. The source command is crucial for loading configurations, setting variables, and defining functions that persist in your current session.

What is the Linux source Command?

The source command reads and executes commands from a file within the current shell environment. The file is typically a shell script, but it can be any text file containing a series of commands. Unlike executing a script normally (which runs in a subshell), source runs the commands in the current shell, making any changes persist in your session.

Syntax

The syntax of the Linux source command is straightforward

source filename

Alternatively, you can use the dot (.) shorthand

. filename

In both forms, filename represents the name of the file containing commands you want to execute.

Basic Example Setting Environment Variables

Create a script file called myscript.sh

#!/bin/bash
export MY_VAR="Hello World"
echo "Variable set: $MY_VAR"

Execute the script using the source command

source myscript.sh
Variable set: Hello World

After running this command, the MY_VAR environment variable remains available in your current shell session.

Working with Functions

The source command is particularly useful for loading shell functions. Create a file called functions.sh

#!/bin/bash
greet() {
    echo "Hello, $1!"
}

show_date() {
    echo "Current date: $(date)"
}

Source the file and use the functions

source functions.sh
greet "Alice"
show_date
Hello, Alice!
Current date: Mon Dec  4 10:30:22 UTC 2023

Modifying the PATH Variable

Create a script to add directories to your PATH

#!/bin/bash
# Add custom bin directory to PATH
export PATH="$HOME/bin:/usr/local/bin:$PATH"
echo "Updated PATH: $PATH"

Source the script to update your PATH

source update_path.sh

The PATH modification persists in your current shell session.

Common Use Cases

Loading Bash Aliases

Create an aliases file ~/.bash_aliases

alias ll='ls -alF'
alias la='ls -A'
alias grep='grep --color=auto'

Source the aliases file

source ~/.bash_aliases

Activating Python Virtual Environments

# Create a virtual environment
python -m venv myenv

# Activate the virtual environment
source myenv/bin/activate

Loading Configuration Files

Many applications use the source command to load configuration

# Load bash configuration
source ~/.bashrc

# Load profile settings
source ~/.profile

Comparison source vs Direct Execution

Method Execution Context Variable Persistence Function Availability
source script.sh Current shell Yes Yes
./script.sh New subshell No No
bash script.sh New subshell No No

Best Practices

  • Use source when you need changes to persist in your current shell session

  • Always check if the file exists before sourcing it using [ -f filename ]

  • Use the dot notation . filename for POSIX compatibility

  • Be cautious when sourcing untrusted scripts as they run with your current privileges

Conclusion

The Linux source command is a powerful tool for executing scripts in the current shell environment, making it essential for loading configurations, setting environment variables, and defining functions. Unlike normal script execution, source ensures that all changes persist in your current session, making it indispensable for shell customization and environment management.

Updated on: 2026-03-17T09:01:38+05:30

29K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements