Session and utilities
The readonly command
Marks a variable or function as read-only: it cannot be changed or unset.
Most useful options
-aRestricts to indexed arrays: each name must be an indexed array.-ARestricts to associative arrays. If you use -a and -A together, -A wins.-fEach name is a function, which becomes protected against unset -f.-pLists the read-only names in a format that can be reused as input. Without names, it also lists.Copy-ready examples
$ readonly FIJO=7 && echo $FIJOdefines a constant and uses it.Example output
7
$ readonly FIJO=7 && FIJO=8the attempt to change it fails.Example output
bash: FIJO: readonly variable
$ readonly FIJO=7 && declare -p FIJOchecks the attribute with declare -p.Example output
declare -r FIJO="7"
$ readonly -a LISTA=(uno dos) && echo "${LISTA[@]}"marks an indexed array as read-only.Example output
uno dos
$ saluda() { echo hola; }; readonly -f saluda; unset -f saludaprotects a function: it can no longer be removed with unset -f.Example output
bash: unset: saluda: cannot unset: readonly function
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| bash: FIJO: readonly variable | You tried to assign a value to a variable marked as read-only. | It cannot be changed in that session. Use another variable, or define the constant only once. |
| bash: declare: A: readonly variable | You tried to remove the attribute with declare +r. With +r, read-only is not removed. | Open a new terminal: the variable will be defined again without the attribute. |
| bash: readonly: -q: invalid option | readonly has no -q option. It is usually a typo or a mix-up with another command. | Use readonly -p to list, or -a, -A and -f with their names. The help is available with help readonly. |
Typical mistakes
- There is no way to remove the attribute in the same session: declare +r does not remove it and unset does not work either.
- It is protection against mistakes in your script, not a security control: a child process receives a copy of the environment that it can change.
- readonly with no names also lists variables of the shell itself, such as UID or EUID, which are already read-only.
Differences from similar commands
declare -r: sets the same read-only attribute, but declare also accepts other attributes at once, such as -i or -x.- export: passes the variable to the environment of child programs. readonly only protects it; it does not export it.
- unset: removes variables. readonly prevents that for the ones you marked.
FAQ
How do I see which variables are read-only?
With readonly with no arguments or with readonly -p. Each line comes out as declare -r, with the value in quotes.
Can an array be read-only?
Yes, with -a for indexed arrays or -A for associative ones. After that you cannot change any element.
Can I mark a variable as read-only without giving it a value?
Yes: readonly NAME marks it as it is, and you will not be able to assign it anything afterwards.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course