Bird
Raised Fist0
Bash Scriptingscripting~10 mins

Read-only variables (readonly) in Bash Scripting - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Read-only variables (readonly)
Declare variable
↓
Make variable readonly
↓
Try to change variable
↓
Error: cannot modify readonly variable
↓
Use variable as is
↓
End
You declare a variable, mark it readonly, then any attempt to change it causes an error, preserving its value.
Execution Sample
Bash Scripting
myvar="hello"
readonly myvar
myvar="world"
echo "$myvar"
This script sets a variable, makes it readonly, tries to change it (which fails), then prints the variable.
Execution Table
StepCommandActionVariable StateOutputNotes
1myvar="hello"Assign 'hello' to myvarmyvar='hello'Variable created with value 'hello'
2readonly myvarMake myvar readonlymyvar='hello' (readonly)myvar is now readonly
3myvar="world"Attempt to change myvarmyvar='hello' (readonly)bash: myvar: readonly variableError: cannot modify readonly variable
4echo "$myvar"Print myvar valuemyvar='hello' (readonly)helloValue remains unchanged
💡 Script ends after printing 'hello'; modification attempt failed due to readonly.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
myvarundefined'hello''hello' (readonly)'hello' (readonly)'hello' (readonly)
Key Moments - 2 Insights
Why does the variable not change after trying to assign a new value?
Because the variable was made readonly at step 2, any assignment after that (step 3) causes an error and does not change the value, as shown in the execution_table.
Does the script stop when the error occurs at step 3?
No, the script continues after the error, so the echo command at step 4 runs and prints the original value.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'myvar' after step 3?
A'world'
B'hello'
Cundefined
Dempty string
💡 Hint
Check the 'Variable State' column at step 3 in the execution_table.
At which step does the script produce an error about modifying a readonly variable?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the 'Output' and 'Notes' columns in the execution_table for the error message.
If we remove the 'readonly' command at step 2, what will be the output at step 4?
Aworld
Bbash error
Chello
Dempty
💡 Hint
Without readonly, the assignment at step 3 succeeds, changing the variable value.
Concept Snapshot
Declare a variable: myvar="value"
Make it readonly: readonly myvar
Trying to change it later causes an error
Readonly variables keep their value safe
Useful to prevent accidental changes
Full Transcript
This example shows how to create a readonly variable in bash. First, we assign 'hello' to myvar. Then we mark myvar as readonly using the readonly command. When we try to assign 'world' to myvar, bash gives an error and the value stays 'hello'. Finally, echo prints the unchanged value. This protects variables from accidental changes.

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