Readonly built-in
The readonly
built-in provides several operations related to read-only variables:
Making variables read-only
If the -p
(--print
) option is not specified and there are any operands, the built-in makes the specified variables read-only.
Synopsis
readonly name[=value]…
Options
None.
Operands
Operands specify the names and values of the variables to be made read-only. If an operand contains an equal sign (=
), the operand is split into the name and value at the first equal sign. The value is assigned to the variable named by the name. Otherwise, the variable named by the operand is created without a value unless it is already defined, in which case the existing value is retained.
If no operands are given, the built-in prints variables (see below).
Standard output
None.
Printing read-only variables
If the -p
(--print
) option is specified, the built-in prints the names and values of the variables named by the operands in the format that can be evaluated as shell code to recreate the variables. If there are no operands, the built-in prints all read-only variables in the same format.
Synopsis
readonly -p [name…]
readonly
Options
The -p
(--print
) option must be specified to print variables unless there are no operands.
Operands
Operands specify the names of the variables to be printed. If no operands are given, all read-only variables are printed.
Standard output
A command string that invokes the readonly
built-in to recreate the variable is printed for each read-only variable. Note that the command does not include options to restore the attributes of the variable, such as the -x
option to make variables exported.
Also note that evaluating the printed commands in the current shell session will fail (unless the variable is declared without a value) because the variable is already defined and read-only.
For array variables, the built-in invocation is preceded by a separate assignment command since the built-in does not support assigning values to array variables.
Errors
When making a variable read-only with a value, it is an error if the variable is already read-only.
When printing variables, it is an error if an operand names a non-existing variable.
Exit status
Zero if successful, non-zero if an error occurred.
Examples
$ readonly foo='Hello, world!'
$ echo "$foo"
Hello, world!
$ readonly
readonly foo='Hello, world!'
$ foo='Goodbye, world!'
error: error assigning to variable
--> <stdin>:4:1
|
4 | foo='Goodbye, world!'
| ^^^^^^^^^^^^^^^^^^^^^ cannot assign to read-only variable "foo"
|
::: <stdin>:1:10
|
1 | readonly foo='Hello, world!'
| ------------------- info: the variable was made read-only here
|
Compatibility
This built-in is part of the POSIX standard. Printing variables is portable only when the -p
option is used without operands.