Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

readonly - set read-only attributes for variables

&pagelevel(4)&pagelevel

The POSIX shell built-in readonly marks the specified shell variables as read-only, i.e. protects them from being changed by reassignment in the current shell. An error message is issued if any such attempt is subsequently made.

This protection only applies to the current shell. This means that the variable may be reassigned in a subshell or in a parent shell (i.e. when you terminate the current shell). Within the current shell, however, it is not possible to undo this protection.

If you invoke readonly without arguments, a list of all existing read-only variables in the current shell is written to standard output.


Syntax


Format 1:readonly[ name[=value]]...
Format 2:readonly -p



Format 1:readonly[ name[=value]]...


name

Name of the shell variable which you want to protect against change. You may enter any number of shell variables each separated by a space.

name not specified:
readonly writes the names of all shell variables which are protected in the current shell to the standard output. The output has the following form: name=value

Format 2:readonly -p


readonly writes the names of all shell variables which are protected in the current shell to the standard output. The output has the following form:

readonly name=value
...

Error

name: is read only
This error message is issued when you try to assign a value to a protected shell variable.

Locale

The following environment variables affect the execution of readonly:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

Example

Marking the shell variable HOME as read-only in the current shell:

$ readonly HOME
$ readonly
HOME=/USER1
$ sh
$ readonly
$ exit
$ readonly
HOME=/USER1

The HOME variable is not protected in the subshell.

See also

export, env, set, typeset