Bird
Raised Fist0
Bash Scriptingscripting~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main difference between an environment variable and a local variable in a Bash script?
easy
A. Local variables are available to child processes, environment variables are not.
B. Environment variables are available to child processes, local variables are not.
C. Environment variables can only be numbers, local variables can be strings.
D. Local variables are global, environment variables are only inside functions.

Solution

  1. Step 1: Understand environment variables

    Environment variables are set with export and are inherited by child processes.
  2. Step 2: Understand local variables

    Local variables exist only in the current shell or function and are not passed to child processes.
  3. Final Answer:

    Environment variables are available to child processes, local variables are not. -> Option B
  4. Quick Check:

    Environment = exported, local = not exported [OK]
Hint: Remember: export makes variables visible to child processes [OK]
Common Mistakes:
  • Thinking local variables are inherited by child processes
  • Confusing export with local keyword
  • Assuming environment variables are only inside functions
2. Which of the following is the correct way to create a local variable inside a Bash function?
easy
A. VAR=value
B. export VAR=value
C. local VAR=value
D. setlocal VAR=value

Solution

  1. Step 1: Identify local variable syntax in Bash functions

    Inside a function, the keyword local declares a variable limited to that function.
  2. Step 2: Compare options

    export makes variables environment variables, setlocal is not a Bash command, and VAR=value without local is global or inherited.
  3. Final Answer:

    local VAR=value -> Option C
  4. Quick Check:

    Use local inside functions for local variables [OK]
Hint: Use 'local' keyword inside functions for local variables [OK]
Common Mistakes:
  • Using export instead of local inside functions
  • Assuming VAR=value inside function is local
  • Using non-existent commands like setlocal
3. What will be the output of this script?
VAR=hello
function test_func() {
  local VAR=world
  echo $VAR
}
test_func
echo $VAR
medium
A. world hello
B. hello hello
C. world world
D. hello world

Solution

  1. Step 1: Analyze variable inside function

    The function declares a local variable VAR with value 'world', so echo $VAR inside prints 'world'.
  2. Step 2: Analyze variable outside function

    The global VAR remains 'hello', so echo $VAR after function call prints 'hello'.
  3. Final Answer:

    world hello -> Option A
  4. Quick Check:

    Local inside function, global outside [OK]
Hint: Local variables override global only inside functions [OK]
Common Mistakes:
  • Assuming local variable changes global variable
  • Confusing output order
  • Ignoring local keyword effect
4. Identify the error in this script snippet:
export VAR=hello
function test() {
  VAR=world
  local VAR
  echo $VAR
}
test
echo $VAR
medium
A. local must be declared before assigning VAR inside function
B. export cannot be used outside functions
C. echo $VAR inside function will print empty
D. Function syntax is incorrect

Solution

  1. Step 1: Understand local variable declaration order

    In Bash, local VAR must appear before assigning a value to VAR inside the function to make it local.
  2. Step 2: Analyze the script behavior

    Here, VAR=world assigns to global VAR before local VAR is declared, so local VAR is empty and echo $VAR prints empty.
  3. Final Answer:

    local must be declared before assigning VAR inside function -> Option A
  4. Quick Check:

    Declare local before assignment inside functions [OK]
Hint: Declare local variables before assigning values inside functions [OK]
Common Mistakes:
  • Assigning before declaring local causes unexpected behavior
  • Thinking export is invalid outside functions
  • Assuming function syntax is wrong
5. You want to write a Bash script that sets a variable PATH_VAR inside a function and have it available outside the function and in any child processes. Which is the correct approach?
hard
A. Inside the function, use export PATH_VAR=value after declaring local PATH_VAR.
B. Declare local PATH_VAR=value inside the function and then export PATH_VAR outside the function.
C. Use local PATH_VAR=value inside the function and do nothing else.
D. Assign PATH_VAR=value inside the function and then export PATH_VAR inside the function without local.

Solution

  1. Step 1: Understand local vs exported variables

    Declaring a variable as local limits it to the function scope, so it won't be available outside or to child processes.
  2. Step 2: Correct way to export variable from function

    Assign the variable without local inside the function, then use export PATH_VAR inside the function to make it environment variable accessible outside and to child processes.
  3. Final Answer:

    Assign PATH_VAR=value inside the function and then export PATH_VAR inside the function without local. -> Option D
  4. Quick Check:

    Export without local to share variable outside function [OK]
Hint: Don't use local if you want variable exported outside function [OK]
Common Mistakes:
  • Using local and export together inside function
  • Exporting variable only outside function after local declaration
  • Assuming local variables can be exported
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard