0
0
Bash Scriptingscripting~10 mins

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

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