source

shell builtinLinux/Unix
The source command is one of the most frequently used commands in Linux/Unix-like operating systems. source Execute commands from a file in the current shell

Quick Reference

Command Name:

source

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

source [options] [arguments]

Common Use Cases

    Syntax

    source filename [arguments]

    Options

    Parameter Description
    filename The file containing shell commands to execute
    arguments Optional arguments that become positional parameters ($1, $2, etc.) in the sourced script

    Note: The source command has no specific options like --help or --version as it's a shell built-in command. The dot (.) command can be used as an alternative to source in Bash and is the standard in POSIX-compliant shells.

    Examples

    How to Use These Examples

    The examples below show common ways to use the source command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    source ~/.bashrc
    Reload your Bash configuration file.
    . ~/.bash_profile
    Alternative syntax to source your login profile.
    source script.sh arg1 arg2
    Execute script.sh in the current shell with arguments. # Advanced Examples Advanced # Create a simple configuration script
    cat > config.sh << EOF
    export PROJECT_ROOT=/path/to/project export DEBUG_MODE=true alias cdp="cd $PROJECT_ROOT" EOF source config.sh echo $PROJECT_ROOT # Outputs: /path/to/project cdp # Changes to the project directory # Use source to create reusable function libraries
    cat > math_lib.sh << EOF
    add() { echo $(($1 + $2)) } multiply() { echo $(($1 * $2)) } EOF source math_lib.sh result=$(add 5 3) echo "5 + 3 = $result" # Outputs: 5 + 3 = 8 # Use source with conditional logic
    cat > feature_flags.sh << EOF
    if [[ "$ENVIRONMENT" == "production" ]]; then export FEATURE_NEW_UI=false else export FEATURE_NEW_UI=true fi EOF ENVIRONMENT=development source feature_flags.sh echo $FEATURE_NEW_UI # Outputs: true # Use source to load environment-specific configurations source ~/.config/$(hostname).conf # Execute code in a temporary file and then delete it temp_file=$(mktemp) echo "echo 'Running from temp file'; export TEMP_VAR=42" > $temp_file source $temp_file echo $TEMP_VAR # Outputs: 42 rm $temp_file # Create a virtual environment activator source venv/bin/activate # Common use case in Python development

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `source` command is a shell built-in that reads and executes commands from a specified file in the current shell environment. It's one of the most important and frequently used commands for shell customization, environment setup, and script organization. Unlike executing a script directly (which runs in a new subprocess), `source` runs the commands in the current shell process. This key distinction has several important implications: 1. Environment Changes Persist: Any environment variables, functions, or aliases defined in the sourced file remain available in the current shell after the file execution completes. 2. Directory Context Preserved: The current working directory is maintained throughout execution, so `cd` commands in the sourced file affect the current shell's location. 3. Shell State Sharing: The sourced script has access to all variables, functions, and aliases defined in the current shell. 4. Return Statement Effect: A `return` statement in a sourced file exits only the sourced file's execution, not the entire shell (unlike `exit`, which would terminate the shell). The `source` command is also available in most shells using the dot (`.`) shorthand, which is the POSIX-standard syntax. In Bash, both `source` and `.` work identically, though `source` is more readable and explicit. Common use cases for the `source` command include: - Loading shell configuration files like `.bashrc`, `.bash_profile`, or `.zshrc` - Creating and using function libraries that can be shared across multiple scripts - Managing environment variables and settings for different projects or contexts - Implementing modular script design by breaking functionality into separate files - Activating virtual environments or specialized shell contexts - Loading user-specific or machine-specific configuration settings - Implementing plugin systems in shell applications When sourcing a file, you can also pass arguments that become available as positional parameters ($1, $2, etc.) within the sourced file. This allows for greater flexibility and reusability of sourced scripts. It's worth noting some potential risks and best practices when using `source`: - Always review scripts before sourcing them, as they run with your full permissions - Be aware of potential variable name conflicts between your shell and sourced scripts - Use local variables in functions to prevent unintended variable pollution - Remember that error handling works differently in sourced scripts (return vs. exit) The `source` command is a fundamental tool for shell customization and scripting, enabling modular, organized, and efficient shell environments.

    Related Commands

    These commands are frequently used alongside source or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the source command works in different scenarios.

    $ source
    View All Commands