0
0
Bash Scriptingscripting~10 mins

Environment variables vs local variables in Bash Scripting - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Environment variables vs local variables
Start Script
Set local variable
Set environment variable (export)
Call child process
Child sees environment variable
Child does NOT see local variable
End Script
This flow shows how local variables exist only in the current script, while environment variables (exported) are passed to child processes.
Execution Sample
Bash Scripting
name="local"
export city="NewYork"
echo "Local: $name"
echo "Env: $city"
bash -c 'echo "Child sees city: $city"; echo "Child sees name: $name"'
This script sets a local variable and an environment variable, then runs a child bash to show which variables are visible.
Execution Table
StepActionVariable 'name'Variable 'city'Output
1Set local variable name="local"localunset
2Export environment variable city="NewYork"localNewYork
3Print local variablelocalNewYorkLocal: local
4Print environment variablelocalNewYorkEnv: NewYork
5Run child bash to print variableslocal (not passed)NewYork (passed)Child sees city: NewYork Child sees name:
💡 Script ends after child process runs; local variable not passed, environment variable passed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 5 (child)
nameunsetlocallocalunset (not visible)
cityunsetunsetNewYork (exported)NewYork (visible)
Key Moments - 2 Insights
Why does the child bash see the environment variable 'city' but not the local variable 'name'?
Because only variables exported with 'export' become environment variables visible to child processes, as shown in execution_table step 5 where 'city' is visible but 'name' is empty.
Does setting a variable without 'export' make it available to child processes?
No, local variables are only available in the current shell or script. The execution_table step 5 shows the child process cannot see 'name' because it was not exported.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the output?
ALocal: NewYork
BLocal: local
CLocal:
DLocal: name
💡 Hint
Check the 'Output' column at step 3 in the execution_table.
At which step does the variable 'city' become visible to the child process?
AStep 1
BStep 3
CStep 5
DStep 2
💡 Hint
Look at the 'Variable city' column and output at step 5 in the execution_table.
If we remove 'export' from the city variable, what happens in the child process output?
AChild sees city:
BChild sees name: local
CChild sees city: NewYork
DChild sees both variables
💡 Hint
Refer to variable_tracker and execution_table step 5 showing visibility depends on export.
Concept Snapshot
Local variables exist only in the current shell or script.
Environment variables are local variables marked with 'export'.
Only environment variables are passed to child processes.
Use 'export VAR=value' to make a variable visible to child processes.
Without export, variables remain local and invisible outside.
Child processes inherit environment variables but not local variables.
Full Transcript
This lesson shows the difference between local and environment variables in bash scripting. A local variable is set normally and exists only in the current shell. An environment variable is created by exporting a local variable with 'export'. When a child process runs, it can see environment variables but not local variables. The example script sets a local variable 'name' and exports 'city'. The child bash process prints both variables. It sees 'city' but not 'name'. This shows that only exported variables are passed down. Understanding this helps control variable visibility in scripts and subprocesses.