The source command in Linux: executing scripts in the current shell

What is the source command?

The source command (also represented by a single dot .) is a built‑in of shells such as Bash, Zsh, and others that allows reading and executing the contents of a script file within the same shell process. Unlike running a script with ./script.sh or bash script.sh, source does not create a subprocess; therefore, any variable, function, or alias that the script defines remains available in the current shell after execution finishes.

Basic syntax

The simplest form is:

source script_name.sh

or, equivalently:

. script_name.sh

The dot must be followed by a space before the filename. If the script is not in the current directory, you can specify a relative or absolute path.

Why use source instead of running the script normally?

  • Variable persistence: Variables defined inside the script remain in the shell’s environment.
  • Functions and aliases: You can define functions that can then be called directly from the command line.
  • Configuration reload: Files such as .bashrc, .zshrc, or .profile are reloaded with source to apply changes without closing the terminal.
  • Debugging and testing: It allows testing code snippets in the same environment where you work on other projects.

Practical examples

1. Defining and using a variable

Let’s create a file named config.sh with the following content:

# config.sh
MY_VAR='Hello from source'
export ANOTHER_VAR=42

Then, in the terminal:

source config.sh
echo $MY_VAR
echo $ANOTHER_VAR

The output will be:

Hello from source
42

2. Loading functions

In functions.sh:

greet() {
  echo 'Hello, '$1'!'
}

Then, in the terminal:

source functions.sh
greet World

Result:

Hello, World!

3. Reloading shell configuration

If you edit .bashrc and want to apply the changes:

source ~/.bashrc

Limitations and considerations

  • Source only works in shells that implement it as a built‑in (Bash, Zsh, Ksh, etc.). In more minimal shells like dash it may not be available.
  • If the script contains commands that change the working directory (cd) or modify environment variables (export), those changes will affect the current shell, which may be desirable or not depending on the case.
  • It is important to ensure the script is trustworthy, because source executes all its content with the same privileges as the current user.

Conclusion

The source command is an essential tool for any Linux user who wants to maintain a dynamic and efficient working environment. It allows integrating configuration scripts, defining reusable variables and functions, and reloading settings without leaving the current session. Mastering its use improves productivity and avoids unnecessary subprocess creation, making the shell more responsive and adaptable to your needs.

This post is also available in ESPAÑOL.

Leave a Reply

Your email address will not be published. Required fields are marked *

Esta obra está bajo una Licencia Creative Commons Atribución 4.0 Internacional para Francesc Roig francesc@vivaldi.net .