Desk Clarity

A field guide to everyday digital problems.

Developer tools / Practical guide

Export in a Script Does Not Update Its Parent Shell: Choose the Scope

Export makes a variable available to child processes.

By ON Lab2 min readSources checked November 10, 2026 (KST)

Export makes a variable available to child processes. A separately executed script cannot update the environment of the shell that launched it. Source trusted shell code only when changes to the current shell are intended.

Reproduce the behavior safely

Run this self-contained fixture in an empty temporary directory, separate from your project. Shell examples create only named fixture files there. JavaScript examples use local values and send no network requests.

unset ONLAB_SCOPE_DEMO
printf 'export ONLAB_SCOPE_DEMO=ready\n' > settings.sh
bash settings.sh
test -z "${ONLAB_SCOPE_DEMO+x}"
. ./settings.sh
test "$ONLAB_SCOPE_DEMO" = ready
bash -c 'test "$ONLAB_SCOPE_DEMO" = ready'
printf 'current shell: %s\n' "$ONLAB_SCOPE_DEMO"
unset ONLAB_SCOPE_DEMO

Observed local output

current shell: ready

Why the result changes

The child executes the export and then exits. Its parent retains its original environment. Sourcing runs the same commands in the current shell, so that shell gains the variable and can pass it to a later child. The fixture uses an explicit relative path, preventing an unrelated PATH lookup from choosing another file.

Choose and verify the correction

If only one command needs configuration, supply the environment to that command instead of changing an interactive session. If a shell setup file must affect the current session, review it and source it in a compatible shell. Document the distinction in the invocation instructions. For tools that emit configuration, prefer a structured output interface where possible; do not turn arbitrary output into executable code merely to move values across a process boundary.

Limits and next checks

Sourcing can change more than variables: directory, options, functions and even exit behavior can affect the caller. Never source an untrusted downloaded file. This example creates its own reviewed settings file in an isolated directory and runs in a disposable Bash process. It does not provide a universal setup script shared by Bash, csh and other languages.

Related: Bash Prefix Assignment Changes the Child Environment, Not Earlier Argument Expansion.

Isolated fixture executed on macOS on November 10, 2026 (KST), with GNU bash, version 3.2.57(1)-release (arm64-apple-darwin26). The observed output verifies this example, not every application or runtime.

Sources & further reading