LinuxCommandLibrary

source

Execute commands in the current shell environment

TLDR

Evaluate contents of a given file

$ source [path/to/file]
copy

Evaluate contents of a given file (alternatively replacing source with .)
$ . [path/to/file]
copy

SYNOPSIS

source FILENAME [ARGUMENTS...]
. FILENAME [ARGUMENTS...]

DESCRIPTION

The source command (or its POSIX equivalent, . the dot command) is a shell builtin that reads and executes commands from a file in the current shell context. Unlike executing a script directly (e.g., ./myscript.sh), which typically creates a subshell for execution, source ensures that any commands, variable assignments, function definitions, or alias creations within the specified file directly affect the current interactive or non-interactive shell session.
This means changes made to environment variables (e.g., PATH), shell options, or user-defined functions and aliases persist after the script finishes. It is commonly used to reload configuration files like .bashrc, .profile, or .zshrc without needing to restart the shell, or to set up environment variables for a project.

CAVEATS

Security Risk: Sourcing scripts from untrusted sources is highly dangerous as they execute with the full permissions and context of your current shell, potentially modifying your environment, executing arbitrary commands, or even deleting files.
Persistent Changes: Be aware that all changes made by the sourced script (variable assignments, function definitions, aliases, shell options) will remain active in your current shell. This is its primary purpose but can lead to unexpected behavior if not managed carefully.
Error Handling: If the sourced script encounters an error or exits, the sourcing shell will typically continue execution unless specific error handling (like set -e within the sourced script) is in place.
Shell Differences: While . is POSIX compliant across shells (Bash, Zsh, Dash, Ksh), the source command itself is a Bash and Zsh builtin. In sh (which might be a symlink to dash), only . is guaranteed to work.

DIFFERENCE FROM DIRECT EXECUTION

When a script is executed directly (e.g., ./script.sh or bash script.sh), it typically runs in a new subshell. This means any variables, functions, or aliases defined within that script are local to the subshell and disappear once the script finishes. source bypasses this, making its changes persistent in the parent shell.

COMMAND PARAMETERS

FILENAME: The path to the script or file to be executed. If no directory is specified, the shell typically searches the current directory and then directories listed in the PATH environment variable.
ARGUMENTS: Optional arguments passed to the sourced script. These arguments become the positional parameters ($1, $2, etc.) within the sourced script.

RETURN STATUS

The exit status of source (or .) is the exit status of the last command executed within the sourced file. If the file cannot be read or found, the return status is non-zero.

HISTORY

The concept of executing a script in the current shell environment has been fundamental to Unix shells for a long time. The . (dot) command is defined by the POSIX standard and is available in virtually all compliant shells (like Bash, Zsh, Ksh, Dash). The source command, while serving the same purpose, is an extension primarily found as a builtin in shells like Bash and Zsh. Its inclusion makes the command name more descriptive for users coming from other environments or for readability. Its usage has remained consistent: enabling shell configuration and environment setup without spawning new processes.

SEE ALSO

exec(1), export(1), bash(1), sh(1), set(1)

Copied to clipboard