source

In Unix-like operating systems, the source command is used to read and execute commands from a file in the current shell environment. This command is often used in shell scripting and managing environment configurations. Here’s an overview of the source command and its usage:

Overview of source

Purpose: The source command executes commands from a specified file in the current shell. It is commonly used to load environment variables, functions, and configurations into the current shell session.

Shell Compatibility: The source command is built into most Unix-like shells such as bash, zsh, and ksh. In csh and tcsh, the equivalent command is . (dot).

Usage

source filename

or

. filename
  • filename: The path to the file containing the commands to be executed.

Common Scenarios for Using source

  1. Loading Environment Variables:

    • You can define environment variables in a file and use source to load them into the current shell:

      # envvars.sh
      export PATH=$PATH:/usr/local/bin
      export EDITOR=nano
      source envvars.sh
  2. Setting Up Aliases and Functions:

    • Aliases and functions defined in a script can be loaded into the current shell session:

      # aliases.sh
      alias ll='ls -l'
      alias la='ls -a'
      source aliases.sh
  3. Sourcing Configuration Files:

    • Configuration files like .bashrc or .profile are often sourced to apply settings to the shell:

      source ~/.bashrc
  4. Running Initialization Scripts:

    • Initialization scripts that set up the environment for specific tasks can be sourced:

      # setup.sh
      export PROJECT_HOME=/home/user/project
      cd $PROJECT_HOME
      source setup.sh

Important Notes

  • Current Shell Context: Unlike running a script directly (e.g., ./script.sh), which executes in a new shell, source runs commands in the current shell context. This means any changes to environment variables, working directory, or shell options will affect the current shell session.

  • Error Handling: If the sourced file contains errors, they will be reported in the current shell. The execution will continue after the error unless the errexit option is set (set -e).

  • Portability: While source is widely used in bash, it is not POSIX standard. The dot command (.) is more portable and works in POSIX-compliant shells.

Examples

  1. Simple Example:

    # greetings.sh
    echo "Hello, $USER"
    source greetings.sh

    Output: Hello, <your-username>

  2. Complex Example:

    # setup_env.sh
    export DB_HOST=localhost
    export DB_USER=root
    export DB_PASS=secret
    source setup_env.sh
    echo $DB_HOST  # Output: localhost

Conclusion

The source command is a powerful utility for managing shell environments, loading configurations, and initializing scripts. It plays a crucial role in shell scripting and session management, providing a flexible way to execute commands within the current shell context.

Last updated