A local variable is a variable declared inside a function using the 'local' keyword. It exists only within that function and is not accessible outside it.
Click to reveal answer
beginner
How do you declare a local variable inside a bash function?
Use the 'local' keyword before the variable name, like this: local varname=value.
Click to reveal answer
beginner
Why use local variables inside bash functions?
Local variables prevent conflicts by keeping variables inside functions separate from variables outside. This helps avoid accidental changes to global variables.
Click to reveal answer
intermediate
What happens if you don't use 'local' inside a bash function?
Variables declared without 'local' inside a function are global. They can change values outside the function, which might cause unexpected results.
Click to reveal answer
beginner
Example: What is the output of this script?
myfunc() {
local x=5
echo $x
}
myfunc
echo $x
Output:
5
(empty line)
Explanation: Inside the function, 'x' is local and prints 5. Outside, 'x' is not set, so echo prints nothing.
Click to reveal answer
What keyword is used to declare a local variable inside a bash function?
Alocal
Bvar
Cdeclare
Dfunction
✗ Incorrect
The 'local' keyword declares a variable that exists only inside the function.
If a variable is declared inside a function without 'local', what is its scope?
ALocal to the function only
BTemporary for one command
CRead-only
DGlobal to the script
✗ Incorrect
Without 'local', variables inside functions are global and affect the whole script.
Why is it good practice to use local variables in bash functions?
ATo make variables accessible everywhere
BTo make variables read-only
CTo avoid variable name conflicts
DTo speed up the script
✗ Incorrect
Local variables keep function variables separate, avoiding conflicts with global variables.
What will this script print?
func() {
local a=10
echo $a
}
func
echo $a
A10 and empty line
B10 and 10
Cempty line and 10
Dempty line and empty line
✗ Incorrect
Inside the function, 'a' is 10. Outside, 'a' is not set, so echo prints nothing.
Which of these is true about local variables in bash?
AThey keep their value after the function ends
BThey exist only during the function execution
CThey can be accessed outside the function
DThey are global by default
✗ Incorrect
Local variables exist only while the function runs and disappear after it finishes.
Explain what a local variable is in bash and why you would use the 'local' keyword inside a function.
Think about how variables inside functions can affect the rest of the script.
You got /4 concepts.
Describe what happens if you declare a variable inside a bash function without using 'local'. How does it affect the script?
Consider the difference in variable scope with and without 'local'.
You got /4 concepts.
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
Step 1: Understand variable scope in Bash functions
Variables declared with local exist only inside the function where they are declared.
Step 2: Compare with other variable types
Global variables exist outside functions; local prevents changes outside the function.
Final Answer:
It creates a variable that exists only inside the function. -> Option D
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
Step 1: Recall correct syntax for local variables
The correct syntax is local variable_name=value.
Step 2: Check each option
Only local count=10 matches the correct syntax.
Final Answer:
local count=10 -> Option C
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
Step 1: Analyze local variable inside function
Inside test_func, value is local and set to 10, so echo $value prints 10.
Step 2: Check global variable outside function
Outside the function, value remains 5 because local variable does not affect it.
Final Answer:
10
5 -> Option A
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
Step 1: Check variable naming rules
Using local as a variable name conflicts with the reserved keyword local.
Step 2: Understand syntax error cause
Assigning local=5 tries to overwrite the keyword, causing a syntax error.
Final Answer:
Using 'local' as a variable name causes a syntax error. -> Option A
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
Step 1: Understand local variable purpose
Declaring local counter inside the function creates a separate variable that does not affect the global one.
Step 2: Check other options
Incrementing global directly changes global variable; export shares variables with child processes; declaring local outside function is invalid.
Final Answer:
Declare local counter inside the function and increment it there. -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.