0
0
Bash Scriptingscripting~20 mins

Unsetting variables (unset) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Unset Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output after unsetting a variable?
Consider the following Bash script snippet:
var="Hello"
unset var
echo "$var"

What will be the output when this script runs?
Bash Scripting
var="Hello"
unset var
echo "$var"
A(no output — empty line)
BHello
Cunset: var: not found
DError: variable var is unset
Attempts:
2 left
💡 Hint
After unsetting, the variable no longer exists.
💻 Command Output
intermediate
2:00remaining
What happens when unsetting an array element?
Given this Bash code:
arr=(one two three)
unset arr[1]
echo "${arr[@]}"

What is the output?
Bash Scripting
arr=(one two three)
unset arr[1]
echo "${arr[@]}"
Aone three
Bone two three
Cone three
DError: cannot unset array element
Attempts:
2 left
💡 Hint
Unsetting an array element removes it, shifting does not happen.
📝 Syntax
advanced
2:00remaining
Which option correctly unsets multiple variables in one command?
Select the correct Bash command to unset variables var1, var2, and var3 at once.
Aunset [var1 var2 var3]
Bunset(var1, var2, var3)
Cunset var1; var2; var3
Dunset var1 var2 var3
Attempts:
2 left
💡 Hint
The unset command accepts multiple variable names separated by spaces.
🔧 Debug
advanced
2:00remaining
Why does this unset command fail?
Given this script snippet:
myvar="data"
unset "$myvar"
echo "$myvar"

Why does the variable still print 'data' after unsetting?
ABecause unset cannot unset variables with quotes
BBecause unset "$myvar" tries to unset a variable named 'data', not 'myvar'
CBecause unset only works on environment variables
DBecause unset requires the variable name without $ sign
Attempts:
2 left
💡 Hint
Check what the argument to unset actually is.
🚀 Application
expert
2:00remaining
How many variables remain after this script runs?
Consider this Bash script:
var1=10
var2=20
var3=30
unset var2
var4=40
unset var5

How many variables named var1, var2, var3, var4, var5 exist after running?
A3 variables: var1, var3, var4
B4 variables: var1, var3, var4, var5
C5 variables: var1, var2, var3, var4, var5
D2 variables: var1, var3
Attempts:
2 left
💡 Hint
Unset removes variables if they exist; unsetting a non-existent variable does nothing.