Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Bash source and . commands

Reference for the Bash source (.) builtin: run a script in the current shell, load functions and variables, pass arguments, and avoid common pitfalls.

source FILE (and its POSIX spelling . FILE) reads a file and executes its commands in the current shell, not in a child process. Any variable, function, alias or directory change the file makes stays after it finishes. That is exactly what you want for configuration files and function libraries, and exactly what you do not get from bash FILE or ./FILE.

Because there is no new process, the file does not need execute permission, and it does not need a shebang line.

Terminal
source FILENAME [ARGUMENTS]
. FILENAME [ARGUMENTS]

Try it

Bash Why sourcing differs from running

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Arguments, return and search path

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
source filename [arguments]
. filename [arguments]
Name Description
filename File to read. With a slash it is used as a path; without one, Bash looks in the directories of PATH and, if not found there, in the current directory (unless posix mode is on).
arguments If given, they become the positional parameters ($1, $2, ...) while the file runs and are restored afterwards. Without arguments the caller's positional parameters stay visible.

Returns: the exit status of the last command in the file, or the number given to return inside it. If the file cannot be found or read, the status is 1.

Errors and edge cases: in a non-interactive shell, a missing file stops the script when posix mode is on; in normal Bash mode the script continues and prints an error. An exit inside a sourced file exits the whole shell.

Usage notes

  • Same shell, same scope. Variables set in the file are global unless declared with local inside a function. This is why source ~/.bashrc applies your new aliases at once.
  • return versus exit. Use return to leave a sourced file; exit ends your terminal session or the calling script.
  • Sourcing untrusted files is code execution. Never source a file that someone else can write to or that is built from user input. For plain KEY=value settings, parse them with read or grep instead.
  • Missing newline at the end of the file is fine; syntax errors, however, stop the file at that point and leave earlier commands applied.
  • Paths. When a script sources a sibling file, build the path from the script location ("$(dirname "${BASH_SOURCE[0]}")/lib.sh") rather than the current directory, which depends on how the script was called.
  • Environment files. set -a; source .env; set +a exports every variable defined in .env; see set and export.
  • Not the same as exec or bash. exec replaces the shell, bash file starts a new shell, source file borrows the current one (exec).
  • Shell compatibility. . works in every POSIX shell; source is a Bash/Zsh/Ksh extension, so use . in /bin/sh scripts. Passing arguments to a dot-sourced file is not guaranteed by POSIX, though Bash and dash support it.