readonly

shell builtinLinux/Unix
The readonly command is one of the most frequently used commands in Linux/Unix-like operating systems. readonly Mark variables or functions as read-only

Quick Reference

Command Name:

readonly

Category:

shell builtin

Platform:

Linux/Unix

Basic Usage:

readonly [options] [arguments]

Common Use Cases

    Syntax

    readonly [options] [name[=value] ...]

    Options

    Option Description
    -a Each name refers to an indexed array variable
    -A Each name refers to an associative array variable (Bash 4+)
    -f Each name refers to a shell function
    -p Display a list of all readonly variables or functions
    -- End option processing

    Examples

    How to Use These Examples

    The examples below show common ways to use the readonly command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    Basic Examples:

    readonly VAR1="value"
    Define and mark VAR1 as read-only with an initial value.
    readonly VAR2
    Mark existing variable VAR2 as read-only.

    Advanced Examples:

    readonly -p
    Display all read-only variables and their values.
    readonly -a ARRAY=(1 2 3 4) Create a read-only array. readonly -A HASH=([key1]="value1" [key2]="value2") Create a read-only associative array (requires Bash 4+). readonly -f function_name Mark a function as read-only. readonly PATH Make the PATH environment variable read-only (use with caution). VAR1="initial" readonly VAR1 VAR1="changed" # This will result in an error Attempt to modify a read-only variable. readonly $(grep -v '^#' readonly_vars.txt) Read variable names from a file and mark them as read-only. readonly USER_CONFIG="/etc/app/config" Make configuration paths read-only for consistent reference.

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `readonly` command is a shell built-in that marks variables or functions as read-only, preventing them from being modified or unset for the duration of the shell session. This command is an important tool for creating constants and protecting critical values in shell scripts. When a variable is marked as read-only, any attempt to assign a new value to it or to unset it will result in an error. This provides a level of protection against accidental modification of important values, helping to prevent bugs and ensure script consistency. Key features and uses of the `readonly` command include: 1. Creating Constants: Define values that should never change throughout script execution. 2. Protecting Configuration: Ensure that configuration variables remain unchanged once they are set. 3. Enforcing Invariants: Guarantee that certain assumptions in the script remain valid. 4. Documenting Intent: Clearly indicate which variables are meant to be constants. 5. Function Protection: Prevent critical functions from being redefined. The read-only attribute is only effective within the current shell environment. If a script creates a read-only variable, that protection does not extend to other scripts or subshells unless explicitly exported. However, child processes (subshells) do inherit the read-only attribute for variables that are exported. It's important to note that the read-only attribute cannot be removed during the current shell session. Once a variable or function is marked as read-only, it remains so until the shell exits. If you need to modify a value that might need to be read-only in some contexts, you should carefully design your script to set the read-only attribute at the appropriate time. In shell scripting, `readonly` is commonly used for: - Script configuration values that should be immutable - File paths and directory locations - Version numbers and identifiers - Security-sensitive information - Constants used in calculations or string operations The `readonly` command is available in most POSIX-compliant shells, including Bash, Zsh, and Ksh, though the support for arrays and some options may vary between shell implementations.

    Related Commands

    These commands are frequently used alongside readonly or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the readonly command works in different scenarios.

    $ readonly
    View All Commands