Read-only variables (readonly) in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to run a script changes when using read-only variables in bash.
Specifically, does making a variable read-only affect how long the script takes as it runs?
Analyze the time complexity of the following bash script using readonly variables.
#!/bin/bash
readonly MAX=1000
for ((i=1; i<=MAX; i++)); do
echo "Number: $i"
done
This script sets a read-only variable MAX and loops from 1 to MAX, printing each number.
Look at what repeats in the script.
- Primary operation: The for-loop runs and prints a line each time.
- How many times: It runs MAX times, which is 1000 here.
As MAX grows, the loop runs more times.
| Input Size (MAX) | Approx. Operations |
|---|---|
| 10 | 10 loops and prints |
| 100 | 100 loops and prints |
| 1000 | 1000 loops and prints |
Pattern observation: The number of operations grows directly with MAX. Double MAX, double the work.
Time Complexity: O(n)
This means the time grows in a straight line with the size of MAX. More MAX means more loops.
[X] Wrong: "Making a variable readonly makes the loop run faster or slower."
[OK] Correct: The readonly keyword just prevents changes to the variable. It does not affect how many times the loop runs or how long each loop takes.
Understanding how readonly variables behave helps you write safer scripts without worrying about performance changes. This shows you can focus on correctness and still predict your script's speed.
"What if we replaced the readonly variable with a normal variable that changes inside the loop? How would the time complexity change?"
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
