Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

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

Practice

(1/5)
1. What does the unset command do in a bash script?
easy
A. It removes a variable from the environment, making it undefined.
B. It sets a variable to an empty string but keeps it defined.
C. It prints the value of a variable to the terminal.
D. It creates a new variable with a default value.

Solution

  1. Step 1: Understand the purpose of unset

    The unset command removes a variable completely from the shell environment, so it no longer exists.
  2. Step 2: Compare with other options

    Setting a variable to empty string keeps it defined, printing shows value, creating new variable is unrelated.
  3. Final Answer:

    It removes a variable from the environment, making it undefined. -> Option A
  4. Quick Check:

    unset removes variable = A [OK]
Hint: Unset deletes variable completely, not just empties it [OK]
Common Mistakes:
  • Thinking unset sets variable to empty string
  • Confusing unset with echo or print commands
  • Assuming unset creates variables
2. Which of the following is the correct syntax to unset a variable named myVar in bash?
easy
A. unset =myVar
B. unset myVar
C. unset(myVar)
D. unset:myVar

Solution

  1. Step 1: Recall the syntax of unset command

    The correct syntax is simply unset variable_name without any symbols or parentheses.
  2. Step 2: Check each option

    unset myVar matches the correct syntax. Options B, C, and D use invalid characters or formats.
  3. Final Answer:

    unset myVar -> Option B
  4. Quick Check:

    Correct unset syntax = A [OK]
Hint: Use 'unset variableName' without symbols or parentheses [OK]
Common Mistakes:
  • Adding equal signs or parentheses
  • Using colons or other separators
  • Confusing unset syntax with function calls
3. What will be the output of this bash script?
myVar="Hello"
echo $myVar
unset myVar
echo $myVar
medium
A. Hello
B. Hello Hello
C. olleH
D. Error: variable not found

Solution

  1. Step 1: Analyze the first echo command

    Variable myVar is set to "Hello", so echo $myVar prints "Hello".
  2. Step 2: Understand effect of unset and second echo

    After unset myVar, the variable is removed. The second echo $myVar prints nothing (empty line).
  3. Final Answer:

    Hello -> Option A
  4. Quick Check:

    Unset removes variable, so second echo is empty [OK]
Hint: Unset clears variable, so echo after unset prints nothing [OK]
Common Mistakes:
  • Expecting echo to print old value after unset
  • Thinking unset causes error on echo
  • Confusing unset with setting empty string
4. Identify the error in this script snippet:
unset $myVar
medium
A. No error; script works fine.
B. unset command requires quotes around variable name.
C. Variable name must be uppercase for unset.
D. Using $ before variable name in unset causes error.

Solution

  1. Step 1: Understand how unset expects variable names

    The unset command expects the variable name without the $ sign, which is used for expansion.
  2. Step 2: Analyze the given command

    Using unset $myVar expands $myVar to its value, causing unset to try to remove a variable named as that value, which is incorrect.
  3. Final Answer:

    Using $ before variable name in unset causes error. -> Option D
  4. Quick Check:

    unset needs variable name, no $ [OK]
Hint: Never use $ with variable name in unset command [OK]
Common Mistakes:
  • Adding $ before variable name in unset
  • Quoting variable name unnecessarily
  • Assuming variable case matters for unset
5. You have a script that sets multiple variables:
var1="one"
var2="two"
var3="three"

You want to unset only variables that start with var and end with an odd number (1 or 3). Which command correctly unsets these variables?
hard
A. unset var1 var2 var3
B. unset var2 var3
C. unset var1 var3
D. unset var2

Solution

  1. Step 1: Identify variables starting with 'var' and ending with odd numbers

    Variables are var1, var2, var3. var1 ends with 1 (odd), var3 ends with 3 (odd), var2 ends with 2 (even).
  2. Step 2: Select variables to unset

    Only var1 and var3 match the pattern, so the command should unset these two.
  3. Final Answer:

    unset var1 var3 -> Option C
  4. Quick Check:

    Unset only odd-ending vars = A [OK]
Hint: Unset only variables matching pattern explicitly [OK]
Common Mistakes:
  • Unsetting all variables instead of selective ones
  • Confusing odd and even numbers
  • Using unset with variable values instead of names
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