Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Local variables (local keyword) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Local variables (local keyword)
Start function
↓
Declare local variable
↓
Use local variable
↓
Function ends
↓
Local variable destroyed
↓
Return to main script
↓
Local variable not accessible here
When a function starts, it creates a local variable that exists only inside it. After the function ends, the local variable disappears and cannot be used outside.
Execution Sample
Bash Scripting
my_func() {
  local x=5
  echo "Inside function: x=$x"
}
my_func

echo "Outside function: x=$x"
This script shows a local variable x inside a function and tries to access it outside.
Execution Table
StepActionVariable x ValueOutput
1Function my_func calledundefined
2Declare local x=5 inside function5
3Print x inside function5Inside function: x=5
4Function my_func ends, local x destroyedundefined
5Print x outside functionundefinedOutside function: x=
💡 Function ends, local variable x is destroyed and not accessible outside
Variable Tracker
VariableStartAfter Step 2After Step 4Final
xundefined5undefinedundefined
Key Moments - 2 Insights
Why can't we see the value of x outside the function?
Because x is declared with local inside the function (see step 2 and 5 in execution_table), it only exists inside the function and is destroyed when the function ends.
What happens if we declare x without local inside the function?
Then x becomes a global variable and keeps its value outside the function, unlike the local variable shown in step 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x inside the function after declaration?
Aundefined
B0
C5
Dnull
💡 Hint
Check step 2 and 3 in the execution_table where x is set and printed inside the function.
At which step does the local variable x get destroyed?
AStep 4
BStep 3
CStep 5
DStep 1
💡 Hint
Look at the execution_table row describing function end and variable destruction.
If we remove the local keyword, what will be the output at step 5?
AError
BOutside function: x=5
COutside function:
DOutside function: undefined
💡 Hint
Without local, x becomes global and keeps its value after function ends (compare step 5 output).
Concept Snapshot
local keyword in bash creates variables only inside functions
Local variables disappear after function ends
Outside function, local variables are not accessible
Use local to avoid overwriting global variables
Without local, variables are global by default
Full Transcript
This visual execution shows how the local keyword in bash scripting works. When a function starts, declaring a variable with local means it only exists inside that function. The variable x is set to 5 inside the function and printed there. After the function ends, the local variable x is destroyed and cannot be accessed outside. Trying to print x outside the function shows nothing because x does not exist there. This prevents accidental changes to global variables. If local is removed, x becomes global and keeps its value outside the function. This trace helps beginners see how local variables behave step-by-step.

Practice

(1/5)
1. What does the local keyword do in a Bash function?
easy
A. It deletes the variable after the function ends.
B. It makes the variable global and accessible everywhere.
C. It exports the variable to child processes.
D. It creates a variable that exists only inside the function.

Solution

  1. Step 1: Understand variable scope in Bash functions

    Variables declared with local exist only inside the function where they are declared.
  2. Step 2: Compare with other variable types

    Global variables exist outside functions; local prevents changes outside the function.
  3. Final Answer:

    It creates a variable that exists only inside the function. -> Option D
  4. Quick Check:

    Local variables = function-only scope [OK]
Hint: Local means variable stays inside the function only [OK]
Common Mistakes:
  • Thinking local variables are global
  • Confusing local with export
  • Assuming local deletes variables
2. Which of the following is the correct way to declare a local variable named count inside a Bash function?
easy
A. count local=10
B. var local count=10
C. local count=10
D. declare count local=10

Solution

  1. Step 1: Recall correct syntax for local variables

    The correct syntax is local variable_name=value.
  2. Step 2: Check each option

    Only local count=10 matches the correct syntax.
  3. Final Answer:

    local count=10 -> Option C
  4. Quick Check:

    local + variable = correct syntax [OK]
Hint: Use 'local var=value' inside functions [OK]
Common Mistakes:
  • Placing 'local' after variable name
  • Using 'var' keyword which doesn't exist
  • Mixing 'declare' with 'local' incorrectly
3. What will be the output of this script?
#!/bin/bash
value=5
function test_func() {
  local value=10
  echo $value
}
test_func
echo $value
medium
A. 10 5
B. 5 10
C. 10 10
D. 5 5

Solution

  1. Step 1: Analyze local variable inside function

    Inside test_func, value is local and set to 10, so echo $value prints 10.
  2. Step 2: Check global variable outside function

    Outside the function, value remains 5 because local variable does not affect it.
  3. Final Answer:

    10 5 -> Option A
  4. Quick Check:

    Local inside function, global outside = 10 then 5 [OK]
Hint: Local variable hides global only inside function [OK]
Common Mistakes:
  • Assuming local changes global variable
  • Expecting both outputs to be same
  • Confusing order of printed values
4. Identify the error in this Bash function:
function example() {
  local=5
  echo $local
}
medium
A. Using 'local' as a variable name causes a syntax error.
B. Missing 'local' keyword before variable name.
C. Function missing parentheses after name.
D. No error; script runs fine.

Solution

  1. Step 1: Check variable naming rules

    Using local as a variable name conflicts with the reserved keyword local.
  2. Step 2: Understand syntax error cause

    Assigning local=5 tries to overwrite the keyword, causing a syntax error.
  3. Final Answer:

    Using 'local' as a variable name causes a syntax error. -> Option A
  4. Quick Check:

    Reserved keywords can't be variable names [OK]
Hint: Don't name variables 'local'--it's a reserved keyword [OK]
Common Mistakes:
  • Thinking 'local' can be a variable name
  • Ignoring syntax errors from reserved words
  • Missing parentheses is not the error here
5. You want to write a Bash function that increments a counter variable without affecting the global variable of the same name. Which approach correctly uses local to achieve this?
hard
A. Increment the global variable directly without declaring local.
B. Declare local counter inside the function and increment it there.
C. Use export counter inside the function before incrementing.
D. Declare counter outside the function and use local globally.

Solution

  1. Step 1: Understand local variable purpose

    Declaring local counter inside the function creates a separate variable that does not affect the global one.
  2. Step 2: Check other options

    Incrementing global directly changes global variable; export shares variables with child processes; declaring local outside function is invalid.
  3. Final Answer:

    Declare local counter inside the function and increment it there. -> Option B
  4. Quick Check:

    Local inside function isolates variable changes [OK]
Hint: Use 'local' inside function to isolate variable changes [OK]
Common Mistakes:
  • Modifying global variable unintentionally
  • Misusing export for local scope
  • Declaring local variables outside functions
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