Local Variables vs. Environment Variables
Bash has two main kinds of variables. A local (or shell) variable lives only inside the shell that created it. An environment variable is also passed to every program the shell starts. Choosing between them decides which parts of your system can see a value.
The word "local" has a second, narrower meaning in Bash: variables declared with the local keyword inside a function. This lesson uses "local variable" for the broad meaning, a variable that stays in the shell, and calls out the function case separately.
The Two Kinds Side by Side
Create one variable of each kind and ask a child process what it can see:
name="local_only"
export SHARED="visible_to_children"
cat > peek.sh <<'EOF'
#!/usr/bin/env bash
echo "peek: name=[${name-unset}] SHARED=[${SHARED-unset}]"
EOF
chmod +x peek.sh
./peek.sh
peek: name=[unset] SHARED=[visible_to_children]
peek.sh runs as a separate process. It received SHARED because the variable was exported, and it could not see name. The expansion ${name-unset} prints the word unset when the variable does not exist, which makes the difference visible.
Asking Bash Which Kind a Variable Is
The declare -p command prints a variable together with its attributes. An exported variable carries the -x attribute:
declare -p name SHARED
declare -- name="local_only"
declare -x SHARED="visible_to_children"
The double dash after declare means "no special attributes", so name is a plain shell variable. The -x on SHARED marks it for export. You can also list only exported names with compgen -e:
compgen -e | grep -x -E 'SHARED|name'
SHARED
The -x option of grep requires an exact whole-line match, which prevents partial matches on longer names. Only SHARED is in the exported list.
What Else Can See a Local Variable
A local variable is not completely hidden. It is visible to code that runs inside the same shell or in a copy of it. The boundary is the creation of a new program, not the use of parentheses or a function.
Subshells
A subshell, created with parentheses, is a copy of the current shell. It sees both kinds of variables:
( echo "subshell: name=[$name] SHARED=[$SHARED]" )
subshell: name=[local_only] SHARED=[visible_to_children]
Changes made inside the subshell are lost when it ends, because the subshell is a copy.
Sourced Files
The source command (or a dot) reads a file and runs its commands in the current shell, so there is no child process. A sourced file sees local variables and its changes are kept:
cat > srcpeek.sh <<'EOF'
echo "sourced: name=[${name-unset}] SHARED=[${SHARED-unset}]"
name="changed_by_source"
EOF
source ./srcpeek.sh
echo "after source name=[$name]"
sourced: name=[local_only] SHARED=[visible_to_children]
after source name=[changed_by_source]
Both variables were visible, and the new value of name stayed after the file finished. Running the same file as ./srcpeek.sh would have started a child process that could not see name.
Functions and the local Keyword
A function runs inside the current shell, so it can read and change any variable. The local keyword is the way to create a variable that exists only for the duration of the function call:
f() { local name="inside_f"; echo "f: $name"; }
f
echo "after f: $name"
f: inside_f
after f: changed_by_source
The function used its own name, which shadowed the shell's variable of the same name while it ran. A later lesson on functions covers this in depth. Note that local is about scope within the shell, while export is about visibility to other programs. The two are independent.
A Child Gets a Snapshot
An exported variable is copied to a child process when the child starts. A later change in the parent does not reach a child that is already running:
export STATE=one
bash -c 'sleep 0.3; echo "late child STATE=$STATE"' &
STATE=two
wait
late child STATE=one
The background child started while STATE was one and waited a moment before printing. Meanwhile the parent changed its own STATE to two. The child still printed one, because it holds a private copy. The & runs the command in the background, and wait makes the shell wait for it to finish. Later lessons explain both.
Switching a Variable Between the Two Kinds
A local variable becomes an environment variable when you export it, as lesson 38 shows. The reverse direction uses export -n, which removes the export attribute but keeps the variable and its value in the current shell:
export -n SHARED
./peek.sh
echo "SHARED still in this shell: [$SHARED]"
export SHARED
peek: name=[unset] SHARED=[unset]
SHARED still in this shell: [visible_to_children]
After export -n, the child no longer received SHARED, but the shell kept using the value. The last line restores the export.
Choosing Between the Two
Use these guidelines when you decide what to do with a new variable:
| Situation | Choice |
|---|---|
| A counter, loop index, or temporary result used only in your script | Local variable |
| A value that the script's own functions use | Local variable (or local inside the function) |
A setting that a program you start must read, such as TZ, LANG, or a tool's configuration variable | Environment variable |
| A value that you want to hand to child scripts | Environment variable |
Keeping a variable local avoids surprises. Every exported variable is visible to every program the script runs, so exporting only what is needed keeps the environment small and prevents values from leaking into programs that should not have them. The usual naming convention reflects the difference: environment variables and constants in UPPERCASE, ordinary local variables in lowercase.