Challenge - 5 Problems
Readonly Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate2:00remaining
What is the output of this Bash script using readonly?
Consider this Bash script:
What will be the output when you run it?
readonly VAR=10 VAR=20 echo $VAR
What will be the output when you run it?
Bash Scripting
readonly VAR=10 VAR=20 echo $VAR
Attempts:
2 left
💡 Hint
readonly variables cannot be changed after being set.
✗ Incorrect
The script tries to change a readonly variable VAR. Bash will stop with an error: 'bash: VAR: readonly variable'.
💻 Command Output
intermediate2:00remaining
What happens when you unset a readonly variable in Bash?
Given this script:
What will be the output or error?
readonly MYVAR=5 unset MYVAR echo $MYVAR
What will be the output or error?
Bash Scripting
readonly MYVAR=5
unset MYVAR
echo $MYVARAttempts:
2 left
💡 Hint
readonly variables cannot be unset.
✗ Incorrect
Trying to unset a readonly variable causes an error: 'bash: unset: MYVAR: cannot unset: readonly variable'.
📝 Syntax
advanced2:00remaining
Which option correctly declares a readonly variable with value 'hello' in Bash?
Choose the correct syntax to declare a readonly variable GREETING with value 'hello'.
Attempts:
2 left
💡 Hint
Use the readonly command followed by variable assignment without quotes around the whole expression.
✗ Incorrect
Option D correctly assigns 'hello' to GREETING and marks it readonly. Option D misses quotes (allowed but risky if value has spaces). Option D quotes the whole expression causing syntax error. Option D uses invalid syntax ':='.
🚀 Application
advanced2:00remaining
How to protect a variable from accidental changes in a Bash script?
You want to make sure a variable CONFIG_PATH cannot be changed later in your script. Which approach achieves this?
Attempts:
2 left
💡 Hint
readonly prevents reassignment, export shares variable with child processes.
✗ Incorrect
readonly marks the variable as unchangeable. export shares it with child processes but does not protect from changes. unset removes the variable. declare -i makes it integer type but does not protect it.
🔧 Debug
expert2:00remaining
Why does this Bash script fail to protect the variable with readonly?
Look at this script:
Why does it not prevent changing CONFIG?
CONFIG=default readonly CONFIG CONFIG=custom echo $CONFIG
Why does it not prevent changing CONFIG?
Bash Scripting
CONFIG=default readonly CONFIG CONFIG=custom echo $CONFIG
Attempts:
2 left
💡 Hint
readonly without assignment locks current value, but here it was declared after assignment.
✗ Incorrect
readonly CONFIG locks the current value of CONFIG. Since CONFIG was assigned before readonly, the value 'default' is locked. But the script tries to assign 'custom' after readonly, which causes an error. However, in some shells, readonly without assignment after assignment locks the variable. The error should occur, but if it doesn't, it means readonly was not effective because it was declared without value. The correct explanation is that readonly without assignment locks the variable but the assignment after readonly causes error. So the script should fail at CONFIG=custom. If it doesn't, the readonly was not effective.