A local variable is a variable defined inside a script or function that is only accessible within that script or function. It does not affect or get affected by the outside environment.
Click to reveal answer
beginner
What is an environment variable in bash scripting?
An environment variable is a variable that is available system-wide or to all child processes of the shell. It is used to pass information to programs and scripts.
Click to reveal answer
beginner
How do you create a local variable in bash?
Simply assign a value without the export keyword, for example: myvar="hello". This variable stays local to the current shell or script.
Click to reveal answer
beginner
How do you create an environment variable in bash?
Use the export command before the variable, like export MYVAR="hello". This makes the variable available to child processes.
Click to reveal answer
intermediate
Why use environment variables instead of local variables?
Environment variables let you share data with other programs or scripts started from your shell. Local variables are only for temporary use inside one script or shell session.
Click to reveal answer
Which command makes a variable available to child processes in bash?
Aexport
Blocal
Cset
Ddeclare
✗ Incorrect
The 'export' command marks a variable to be passed to child processes as an environment variable.
What happens if you define a variable without 'export' in bash?
AIt becomes an environment variable
BIt is available to all users
CIt is local to the current shell or script
DIt is deleted immediately
✗ Incorrect
Without 'export', the variable is local to the current shell or script and not passed to child processes.
Which variable type is accessible by all programs started from the shell?
ALocal variable
BEnvironment variable
CTemporary variable
DGlobal variable
✗ Incorrect
Environment variables are passed to all child processes and programs started from the shell.
How do you remove an environment variable in bash?
Aunset variable_name
Bdelete variable_name
Cremove variable_name
Dclear variable_name
✗ Incorrect
The 'unset' command removes a variable, including environment variables.
Which of these is true about local variables in bash?
AThey are shared with all users
BThey persist after the shell closes
CThey require the export command
DThey are only available inside the current shell or script
✗ Incorrect
Local variables exist only inside the current shell or script and are not exported.
Explain the difference between environment variables and local variables in bash scripting.
Think about where each variable can be accessed.
You got /4 concepts.
Describe how to create and use an environment variable in a bash script.
Focus on the export command and its effect.
You got /4 concepts.
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
Step 1: Understand environment variables
Environment variables are set with export and are inherited by child processes.
Step 2: Understand local variables
Local variables exist only in the current shell or function and are not passed to child processes.
Final Answer:
Environment variables are available to child processes, local variables are not. -> Option B
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
Step 1: Identify local variable syntax in Bash functions
Inside a function, the keyword local declares a variable limited to that function.
Step 2: Compare options
export makes variables environment variables, setlocal is not a Bash command, and VAR=value without local is global or inherited.
Final Answer:
local VAR=value -> Option C
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
Step 1: Analyze variable inside function
The function declares a local variable VAR with value 'world', so echo $VAR inside prints 'world'.
Step 2: Analyze variable outside function
The global VAR remains 'hello', so echo $VAR after function call prints 'hello'.
Final Answer:
world
hello -> Option A
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
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.
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.
Final Answer:
local must be declared before assigning VAR inside function -> Option A
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
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.
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.
Final Answer:
Assign PATH_VAR=value inside the function and then export PATH_VAR inside the function without local. -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.