0
0
Bash Scriptingscripting~15 mins

Read-only variables (readonly) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Read-only Variables in Bash Scripts
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo "$config_path" to print the variable's value.