A readonly command is a shell built-in that marks variables as immutable after assignment. Once a name is declared readonly, later attempts to change or unset it fail in that shell, which helps protect configuration values and other shell state from accidental overwrite.
What a readonly command does in a shell
A readonly command creates an immutability boundary inside the current shell session. After a variable is marked readonly, later assignment or unset operations fail for that name, which makes it useful for protecting configuration values, constants, and other shell state that should not be changed accidentally during script execution.
The practical value is not cryptographic protection or tamper resistance. It is a language-level safeguard that reduces accidental mutation in interactive shells and scripts, especially where later commands, sourced files, or helper functions might otherwise overwrite important state without warning.
How readonly affects shell variables and execution flow
Readonly status applies after the name is declared, so the shell continues to use the existing value but blocks subsequent modification attempts. That means the command changes the variable's lifecycle rather than its meaning: the value remains available, but its state becomes fixed for the rest of that shell process.
This matters in scripts that rely on stable parameters, environment-derived settings, or internal constants. It can also surface errors earlier, because a failed reassignment is visible when the shell tries to change a protected name instead of silently continuing with an unintended value.
Common failure modes and limitations
Readonly is easy to misunderstand as a full integrity control, but it only constrains that shell's own variable handling. It does not protect the value from being copied elsewhere, exported to another process, reintroduced in a new shell, or changed before readonly is applied.
It also offers no protection if the shell script itself is compromised or if the constant is established from already-untrusted input. In other words, readonly helps with accidental overwrite, not with trust validation, input sanitation, or broader secret handling.
When to use readonly in scripts
Use readonly for names that should remain stable for the lifetime of the shell session, such as fixed paths, feature flags, mode selectors, or helper values that drive later logic. It is most helpful when the script has multiple execution branches, sourced files, or functions that could otherwise reuse a variable name by mistake.
Choose it when the goal is to prevent accidental state drift, not to enforce policy across a system. If a value is security-sensitive, readonly can be one supporting safeguard, but it should be paired with the right handling for the underlying data rather than treated as a standalone control.
Practitioner Guidance
Why practitioners should care: Readonly is a small but useful defensive habit in shell scripting because it turns accidental reassignment into an immediate error. That makes scripts easier to reason about, especially when constants are shared across functions or sourced fragments.
Common misunderstanding: marking a variable readonly does not make the value trustworthy or private, and it does not substitute for validation. It simply narrows who can change that name inside the current shell context.
Practitioner takeaway: treat readonly as a guardrail for script correctness, then pair it with explicit validation and careful variable naming where the value affects control flow or privileged operations.
Related resources from NHI Mgmt Group
Deepen Your Knowledge
Free weekly newsletter
Subscribe to the NHI & AI Identity Journal
The latest on NHI and Agentic AI security – articles, research, breaches, news and events every week.
Bonus 33% off our NHI Course when you subscribe.
Reviewed and updated by the NHIMG editorial team on November 9, 2026.
NHI Mgmt Group — the #1 independent authority on Non-Human Identity, IAM, and Agentic AI security. nhimg.org