How to Use the Source Command in Bash

Updated on 31 July, 2025
Learn how to use the source command in Bash to load configs, variables, and functions into your shell session.
How to Use the Source Command in Bash header image

The source command is a Bash shell builtin that executes commands from a file in your active shell environment, rather than launching a new subshell. Unlike running a script directly, source reads and evaluates the file's contents in your active shell session. As a result, any variables, functions, or environment changes take effect immediately.

This article covers common use cases for the source command, including loading configuration files, debugging scripts with existing shell variables, importing functions, and setting environment variables. You will learn the syntax and practical applications that make source an essential tool for Bash scripting and system administration.

Common Uses for the source Command

Use the source command to execute a script in your active shell environment. This allows any environment changes or variable assignments to persist after the script runs.

The basic syntax is:

source filename [arguments]

You can also use the shorthand dot (.) notation:

. filename [arguments]

When you provide arguments, they become positional parameters ($1, $2, $3, and so on) inside the sourced script. This makes it possible to pass configuration values or options dynamically.

Loading Configuration Files

Use the source command to load configuration files that define environment variables, aliases, or other shell customizations. This approach is useful for making session-specific changes that take effect immediately.

Command Syntax

source path/to/config_file
  • path/to/config_file: Path to a file containing shell-compatible commands, such as export, alias, or function definitions.

You can also use the shorthand version:

. path/to/config_file

Command Demonstration

  1. Create a new configuration file named .my_config.

    console
    $ nano ~/.my_config
    
  2. Add the following lines to define an environment variable and an alias:

    ini
    export MY_APP_PATH="/opt/myapp"
    alias ll='ls -la'
    

    Save and close the file.

  3. Source the configuration file to apply the changes.

    console
    $ source ~/.my_config
    
  4. Verify that the variable and alias are now available in the shell.

    console
    $ echo $MY_APP_PATH && ll
    

    Output:

    /opt/myapp
    total 21
    drwxr-xr-x  2 user user 4096 Dec 15 10:30 .
    drwxr-xr-x 18 user user 4096 Dec 15 10:25 ..
    -rw-r--r--  1 user user  220 Dec 15 10:30 .my_config
    ...

Debugging with Existing Shell Variables

Use the source command to test scripts that rely on environment variables or need to modify the current shell state. Unlike direct execution, sourcing ensures that changes made inside the script persist in the calling shell.

Command Syntax

source script.sh
  • script.sh: A script that references or modifies environment variables in the current shell session.

Command Demonstration

  1. Set an environment variable in your current shell.

    console
    $ export MY_VAR="Original value"
    
  2. Create a script named test.sh.

    console
    $ nano test.sh
    
  3. Add the following content:

    bash
    #!/bin/bash
    
    echo "MY_VAR is: $MY_VAR"
    MY_VAR="Modified value"
    echo "MY_VAR is now: $MY_VAR"
    

    Save and close the file.

  4. Make the script executable.

    console
    $ chmod +x test.sh
    
  5. Run the script directly (in a subshell).

    console
    $ ./test.sh
    

    Output:

    MY_VAR is: Original value
    MY_VAR is now: Modified value
  6. Check the variable in your shell.

    console
    $ echo "MY_VAR is: $MY_VAR"
    

    Output:

    MY_VAR is: Original value

    The variable remains unchanged because the script ran in a subshell.

  7. Source the script to execute it in your shell.

    console
    $ source test.sh
    

    Output:

    MY_VAR is: Original value
    MY_VAR is now: Modified value
  8. Check the variable again.

    console
    $ echo "MY_VAR is: $MY_VAR"
    

    Output:

    MY_VAR is: Modified value

Loading Functions in Existing Scripts

Use the source command to load functions from an external file into your current shell session. This makes custom utilities reusable without rewriting code across multiple scripts.

Command Syntax

source path/to/functions_file.sh
  • functions_file.sh: A file that contains function definitions written in Bash syntax.

Command Demonstration

  1. Create a function library file named functions.sh.

    console
    $ nano functions.sh
    
  2. Add the following function definition:

    bash
    #!/bin/bash
    
    system_info() {
       echo "System Information:"
       echo "==================="
       echo "Hostname: $(hostname)"
       echo "Uptime: $(uptime)"
    }
    

    Save and close the file.

  3. Source the file to load the function into your shell.

    console
    $ source functions.sh
    
  4. Call the function directly.

    console
    $ system_info
    

    Output:

    System Information:
    ===================
    Hostname: ubuntu-server
    Uptime: up 2 hours, 34 minutes, 3 users, load averages: 4.20 4.41 3.87

Setting Environment Variables

Use the source command to apply multiple environment variables from a single file. This method is useful for application configuration and development environments where consistent variables are required across sessions.

Command Syntax

source environment_file.sh
  • environment_file.sh: A shell script that defines variables using export.

Command Demonstration

  1. Create an environment file named app_env.sh.

    console
    $ nano app_env.sh
    
  2. Add the following variable definitions:

    bash
    #!/bin/bash
    
    export DB_HOST="localhost"
    export DB_PORT="5432"
    export APP_ENV="development"
    export APP_PORT="8080"
    

    Save and close the file.

  3. Source the environment file to apply the variables.

    console
    $ source app_env.sh
    
  4. Create a script that uses the environment variables.

    console
    $ nano test_env.sh
    
  5. Add the following content to the file.

    bash
    #!/bin/bash
    
    echo "Connecting to database at $DB_HOST:$DB_PORT"
    echo "Application running on port $APP_PORT"
    echo "Environment: $APP_ENV"
    

    Save and close the file.

  6. Give the execution permission to the file.

    console
    $ chmod +x test_env.sh
    
  7. Run the script to verify the environment variable usage.

    console
    $ ./test_env.sh
    

    Output:

    Connecting to database at localhost:5432
    Application running on port 8080
    Environment: development
Note
Environment variables set using source persist only in the active shell session where the command is run. To make them permanent, add the source command to your shell's startup file, such as .bashrc, .bash_profile, or .profile.

Conclusion

In this article, you used the source command to load configuration files, debug scripts with environment variables, import shell functions, and apply environment variables from external files. Unlike scripts that run in subshells, source applies changes immediately to your current shell session, making it a powerful tool for managing environment state and reusable configurations.

Tags:

Comments

No comments yet.