What is the difference between source and executing a script?

Answer

When you execute a script (./script.sh or bash script.sh), a new child process (subshell) is created. Variables set in the script, directory changes, and function definitions do NOT persist in the calling shell after it finishes. When you source a script (source script.sh or . script.sh), the script runs in the current shell process — all variable assignments, function definitions, and cd commands affect the current environment. Sourcing is essential for configuration files (~/.bashrc), loading environment variables, activating virtual environments, and defining shell functions you want to use interactively.