What if a tiny mistake changed your script's key settings without you noticing?
Why Read-only variables (readonly) in Bash Scripting? - Purpose & Use Cases
Imagine you write a script to set important values like a password or a configuration path. You share it with others, but someone accidentally changes those values later in the script or while running it.
Without protection, variables can be changed by mistake or on purpose, causing your script to behave wrongly or fail. Tracking down where the value changed can be frustrating and time-consuming.
Using read-only variables locks their values so they cannot be changed after setting. This keeps your important data safe and your script reliable.
config_path="/usr/local/app" # later someone does config_path="/tmp/hacked"
readonly config_path="/usr/local/app" # trying to change now causes error config_path="/tmp/hacked" # error
It enables you to protect critical values in your scripts, preventing accidental or harmful changes.
In deployment scripts, setting a read-only variable for the server address ensures no part of the script can accidentally redirect commands to the wrong server.
Read-only variables prevent accidental changes.
They make scripts more reliable and easier to debug.
Protect important data by marking variables as readonly.
Practice
readonly command do in a bash script?Solution
Step 1: Understand the purpose of
Thereadonlyreadonlycommand 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 CQuick Check:
readonly= makes variable unchangeable [OK]
- Thinking readonly deletes the variable
- Confusing readonly with printing variable
- Assuming readonly allows changes
MYVAR read-only in bash?Solution
Step 1: Recall the syntax of
The correct syntax is to first assign the variable, then usereadonlyreadonly MYVARto make it read-only.Step 2: Analyze each option
readonly MYVAR =value tries to assign and readonly together, which is invalid. readonly MYVAR correctly usesreadonly MYVAR. Options C and D are not valid bash syntax.Final Answer:
readonly MYVAR -> Option BQuick Check:
Usereadonly VARafter assignment [OK]
- Trying to assign value inside readonly command
- Placing readonly after variable name
- Using invalid commands like set readonly
MYVAR=hello readonly MYVAR MYVAR=world echo $MYVAR
Solution
Step 1: Understand the script flow
The variableMYVARis 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 ofMYVARremains "hello". Theechocommand outputs the original value.Final Answer:
hello -> Option DQuick Check:
Readonly variable value remains unchanged; echo prints original value [OK]
- Assuming variable changes silently
- Expecting syntax error instead of runtime error
- Thinking echo prints new value
readonly MYVAR =10 MYVAR=20 echo $MYVAR
Solution
Step 1: Check the syntax of readonly with assignment
In bash,readonlycannot be combined with assignment likereadonly 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 afterreadonly, not assignments.Final Answer:
Cannot assign value during readonly declaration -> Option AQuick Check:
Assign first, then readonly VAR separately [OK]
- Trying to assign value inside readonly command
- Ignoring syntax error messages
- Confusing export with readonly
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?Solution
Step 1: Assign the variable before making it readonly
You must first assignCONFIG_PATHa value, then usereadonlyto 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 AQuick Check:
Assign, then readonly, then print [OK]
- Trying to assign inside readonly
- Printing variable before assignment
- Making variable readonly before assignment
Ready to prove it?
Pass the test and this pattern moves unknown
