📖 Scenario: You are writing a bash script to manage a simple configuration where some values must not change after being set. This is common in scripts that rely on fixed settings like file paths or version numbers.
🎯 Goal: Learn how to create read-only variables in bash so that their values cannot be changed later in the script.
📋 What You'll Learn
Create a variable with a fixed value
Make the variable read-only using the readonly command
Try to change the read-only variable (to see it fail)
Print the value of the read-only variable
💡 Why This Matters
🌍 Real World
Many bash scripts use read-only variables to protect important settings from accidental changes during execution.
💼 Career
Understanding read-only variables helps you write safer and more reliable automation scripts in system administration and DevOps roles.
Progress0 / 4 steps
1
Create a variable with a fixed value
Create a variable called config_path and set it to the string /etc/myapp/config.
Bash Scripting
Hint
Use = to assign the value without spaces, and wrap the path in double quotes.
2
Make the variable read-only
Use the readonly command to make the variable config_path read-only.
Bash Scripting
Hint
Type readonly followed by the variable name.
3
Try to change the read-only variable
Try to assign the string /tmp/config to the variable config_path after it is made read-only.
Bash Scripting
Hint
Just write the assignment line again after readonly.
4
Print the value of the read-only variable
Print the value of config_path using echo.
Bash Scripting
Hint
Use echo "$config_path" to print the variable's value.
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
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.
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.
Final Answer:
It makes a variable unchangeable after assignment. -> Option C
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
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.
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.
Final Answer:
readonly MYVAR -> Option B
Quick Check:
Use readonly VAR after assignment [OK]
Hint: Assign first, then use readonly VAR to lock it [OK]
The variable MYVAR is assigned "hello" and then made readonly. The script then tries to change it to "world".
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.
Final Answer:
hello -> Option D
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
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.
Step 2: Analyze the error caused by this syntax
This syntax causes an error because bash expects only variable names after readonly, not assignments.
Final Answer:
Cannot assign value during readonly declaration -> Option A
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
Step 1: Assign the variable before making it readonly
You must first assign CONFIG_PATH a value, then use readonly to lock it.
Step 2: Print the variable after it is readonly
Printing the variable after making it readonly is valid and will show the assigned value.
Final Answer:
CONFIG_PATH=/etc/config
readonly CONFIG_PATH
echo $CONFIG_PATH -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.