readonly

2024-07-11

Understanding readonly

The readonly command declares a shell variable as read-only. Once a variable is declared as readonly, any attempt to modify its value will result in an error. This feature protects critical configuration settings or environment variables from unintended changes, promoting safer scripting and better code management.

Syntax and Usage

The basic syntax is straightforward:

readonly variable_name[=value]

Practical Examples

Let’s look at readonly with many illustrative examples:

Example 1: Creating and Declaring a Read-Only Variable

readonly MY_CONSTANT="Hello, world!"
echo $MY_CONSTANT  # Output: Hello, world!
MY_CONSTANT="Goodbye!" # This will result in an error: "MY_CONSTANT: readonly variable"

This demonstrates creating MY_CONSTANT and immediately setting it as read-only. Attempting to change its value subsequently fails.

Example 2: Making an Existing Variable Read-Only

MY_VARIABLE="Initial Value"
readonly MY_VARIABLE
echo $MY_VARIABLE  # Output: Initial Value
MY_VARIABLE="New Value" # This will also result in an error

Here, MY_VARIABLE is first assigned a value, then declared read-only. The subsequent attempt to reassign it generates an error.

Example 3: Declaring Multiple Read-Only Variables

readonly VAR1="Value 1" VAR2="Value 2" VAR3="Value 3"
echo $VAR1 $VAR2 $VAR3 # Output: Value 1 Value 2 Value 3

readonly can handle multiple variable declarations in a single command, improving code efficiency.

Example 4: Using readonly in shell scripts:

This enhances security and prevents accidental overwrites in your scripts.

#!/bin/bash
readonly DATABASE_PASSWORD="mysecurepassword"

Example 5: Checking for Read-Only Variables:

While there isn’t a direct command to check if a variable is readonly, you can indirectly do so by attempting a modification and checking the exit status.

MY_VAR="test"
readonly MY_VAR

MY_VAR="modified"
echo $? # Outputs 1 (indicating an error)

unset MY_VAR
echo $? # Outputs 1 (indicating failure to unset a readonly variable)

The $? variable holds the exit status of the previous command. A non-zero exit status indicates an error.

Example 6: set -r (a shorthand):

The set -r command within the shell activates the readonly option for all subsequently defined variables. Use cautiously!

set -r
MY_NEW_VAR="Test"
MY_ANOTHER_VAR="Another Test"

This is a powerful, but potentially dangerous option. Be mindful when employing this method. Use set +r to turn this setting off.

These examples showcase the flexibility and utility of the readonly command in various shell scripting scenarios, thereby enhancing script robustness and preventing unintended modifications of variables.