Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Read-only variables (readonly) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Readonly Variable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this Bash script using readonly?
Consider this Bash script:
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
A10
B20
Cbash: VAR: readonly variable
DEmpty output
Attempts:
2 left
💡 Hint
readonly variables cannot be changed after being set.
💻 Command Output
intermediate
2:00remaining
What happens when you unset a readonly variable in Bash?
Given this script:
readonly MYVAR=5
unset MYVAR
echo $MYVAR

What will be the output or error?
Bash Scripting
readonly MYVAR=5
unset MYVAR
echo $MYVAR
A5
Bbash: unset: MYVAR: cannot unset: readonly variable
CEmpty output
Dbash: MYVAR: unbound variable
Attempts:
2 left
💡 Hint
readonly variables cannot be unset.
📝 Syntax
advanced
2: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'.
Areadonly GREETING:='hello'
Breadonly GREETING=hello
Creadonly 'GREETING=hello'
Dreadonly GREETING='hello'
Attempts:
2 left
💡 Hint
Use the readonly command followed by variable assignment without quotes around the whole expression.
🚀 Application
advanced
2: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?
ASet CONFIG_PATH and then run: readonly CONFIG_PATH
BSet CONFIG_PATH and then run: export CONFIG_PATH
CSet CONFIG_PATH and then run: unset CONFIG_PATH
DSet CONFIG_PATH and then run: declare -i CONFIG_PATH
Attempts:
2 left
💡 Hint
readonly prevents reassignment, export shares variable with child processes.
🔧 Debug
expert
2:00remaining
Why does this Bash script fail to protect the variable with readonly?
Look at this script:
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
Areadonly was set without assigning a value, so CONFIG remains changeable
Breadonly only works on exported variables
CCONFIG=custom is ignored silently by Bash
Dreadonly must be declared before any assignment
Attempts:
2 left
💡 Hint
readonly without assignment locks current value, but here it was declared after assignment.

Practice

(1/5)
1. What does the readonly command do in a bash script?
easy
A. It prints the value of a variable.
B. It deletes a variable from the script.
C. It makes a variable unchangeable after assignment.
D. It allows a variable to be changed multiple times.

Solution

  1. Step 1: Understand the purpose of readonly

    The readonly command is used to mark a variable so it cannot be changed later in the script.
  2. Step 2: Compare options with the command's function

    Only "It makes a variable unchangeable after assignment." correctly describes that the variable becomes unchangeable after assignment.
  3. Final Answer:

    It makes a variable unchangeable after assignment. -> Option C
  4. Quick Check:

    readonly = makes variable unchangeable [OK]
Hint: Readonly means variable cannot be changed after set [OK]
Common Mistakes:
  • Thinking readonly deletes the variable
  • Confusing readonly with printing variable
  • Assuming readonly allows changes
2. Which of the following is the correct syntax to make a variable MYVAR read-only in bash?
easy
A. readonly MYVAR =value
B. readonly MYVAR
C. MYVAR readonly
D. set readonly MYVAR

Solution

  1. Step 1: Recall the syntax of readonly

    The correct syntax is to first assign the variable, then use readonly MYVAR to make it read-only.
  2. Step 2: Analyze each option

    readonly MYVAR =value tries to assign and readonly together, which is invalid. readonly MYVAR correctly uses readonly MYVAR. Options C and D are not valid bash syntax.
  3. Final Answer:

    readonly MYVAR -> Option B
  4. Quick Check:

    Use readonly VAR after assignment [OK]
Hint: Assign first, then use readonly VAR to lock it [OK]
Common Mistakes:
  • Trying to assign value inside readonly command
  • Placing readonly after variable name
  • Using invalid commands like set readonly
3. What will be the output of this script?
MYVAR=hello
readonly MYVAR
MYVAR=world
echo $MYVAR
medium
A. world
B. Error: cannot modify readonly variable
C. Syntax error
D. hello

Solution

  1. Step 1: Understand the script flow

    The variable MYVAR is assigned "hello" and then made readonly. The script then tries to change it to "world".
  2. Step 2: Predict the behavior when modifying a readonly variable

    Bash will produce an error message when trying to assign a new value to a readonly variable, but the script will continue and the value of MYVAR remains "hello". The echo command outputs the original value.
  3. Final Answer:

    hello -> Option D
  4. Quick Check:

    Readonly variable value remains unchanged; echo prints original value [OK]
Hint: Changing readonly variable causes error message but value unchanged [OK]
Common Mistakes:
  • Assuming variable changes silently
  • Expecting syntax error instead of runtime error
  • Thinking echo prints new value
4. Identify the error in this script:
readonly MYVAR =10
MYVAR=20
echo $MYVAR
medium
A. Cannot assign value during readonly declaration
B. Variable name is invalid
C. Missing export keyword
D. No error, script runs fine

Solution

  1. Step 1: Check the syntax of readonly with assignment

    In bash, readonly cannot be combined with assignment like readonly MYVAR =10. The variable must be assigned first, then made readonly.
  2. Step 2: Analyze the error caused by this syntax

    This syntax causes an error because bash expects only variable names after readonly, not assignments.
  3. Final Answer:

    Cannot assign value during readonly declaration -> Option A
  4. Quick Check:

    Assign first, then readonly VAR separately [OK]
Hint: Assign before readonly; no assignment inside readonly [OK]
Common Mistakes:
  • Trying to assign value inside readonly command
  • Ignoring syntax error messages
  • Confusing export with readonly
5. You want to protect a variable CONFIG_PATH so it cannot be changed accidentally in your script, but you also want to print its value later. Which sequence of commands is correct?
hard
A. CONFIG_PATH=/etc/config readonly CONFIG_PATH echo $CONFIG_PATH
B. readonly CONFIG_PATH=/etc/config echo $CONFIG_PATH
C. CONFIG_PATH=/etc/config echo $CONFIG_PATH readonly CONFIG_PATH
D. echo $CONFIG_PATH CONFIG_PATH=/etc/config readonly CONFIG_PATH

Solution

  1. Step 1: Assign the variable before making it readonly

    You must first assign CONFIG_PATH a value, then use readonly to lock it.
  2. Step 2: Print the variable after it is readonly

    Printing the variable after making it readonly is valid and will show the assigned value.
  3. Final Answer:

    CONFIG_PATH=/etc/config readonly CONFIG_PATH echo $CONFIG_PATH -> Option A
  4. Quick Check:

    Assign, then readonly, then print [OK]
Hint: Assign first, readonly second, then use variable [OK]
Common Mistakes:
  • Trying to assign inside readonly
  • Printing variable before assignment
  • Making variable readonly before assignment
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