readonly
Make shell variables immutable
TLDR
Set a read-only variable
Mark a variable as read-only
[p]rint the names and values of all read-only variables to stdout
SYNOPSIS
readonly [-afp] [name[=value] ...]
PARAMETERS
name
The name of the variable(s) or function(s) to be declared read-only. If value is provided, the variable is assigned that value before being marked read-only.
-a
Used with name to declare or mark an indexed array variable as read-only. If name is not an array, it is created as an indexed array first.
-A
Used with name to declare or mark an associative array variable as read-only. If name is not an array, it is created as an associative array first.
-f
Mark shell functions as read-only. When used without name arguments, it doesn't list functions; it's typically used with one or more function names to make them read-only.
-p
Display a list of all currently read-only variables and functions in a reusable format. When this option is used, no name arguments are accepted.
DESCRIPTION
The readonly command is a shell built-in that declares or marks shell variables and functions as read-only. Once a variable or function is made read-only, its value cannot be changed, nor can it be unset. Attempting to modify or unset a read-only entity will result in an error message. This command is particularly useful for ensuring that critical configuration parameters, environment variables, or core functions remain constant throughout a script's execution, thereby preventing accidental or malicious alteration. It can also be used to list all currently read-only variables and functions in the current shell session.
CAVEATS
Once a variable or function is declared read-only, there is no shell command to revert this state within the current shell session. It cannot be unset or assigned a new value. This immutability applies to the current shell and its subshells (if exported). A read-only variable cannot be "un-readonly" and then modified; its state is permanent for the shell session.
SCOPE AND INHERITANCE
Read-only variables are local to the current shell by default. If a read-only variable is also exported (e.g., using `export readonly VAR=value` or `readonly -x VAR=value`), it will be inherited by subshells as read-only. However, its read-only status in a subshell is independent of the parent shell; a subshell cannot make a read-only variable from its parent writable, nor can it unset it. Redefining a read-only function in a subshell is also not possible.
USAGE WITH ASSIGNMENTS
You can assign a value to a variable and make it read-only in one step: `readonly MY_VAR="my_value"`. If the variable `MY_VAR` already exists, it must not be read-only to be successfully marked, otherwise an error occurs. This allows for atomic declaration and protection.
HISTORY
The readonly command has been a fundamental built-in feature of POSIX-compliant shells like Bash, Zsh, and Ksh for a very long time, providing a robust mechanism for variable and function immutability. Its design aligns with the principle of ensuring script stability and preventing unintended modifications to critical state.