readonly and unset
In this page:
Declaring a Constant with readonly
readonly NAME=value creates a variable and marks it immutable in one step, so any later attempt to reassign it fails with an error message rather than silently changing the value. This is useful for values that represent true constants in your script, like a fixed configuration path.
Example: Declaring a Constant with readonly
#!/bin/bash
readonly MAX_RETRIES=3
echo "Max retries allowed: $MAX_RETRIES"
Login to try C/C++/Java/PHP code in the editor
Attempting to Change a readonly Variable
If you try to reassign a readonly variable, Bash prints an error to stderr and refuses the assignment, but by default the script continues running afterward (it does not halt automatically). Wrapping the attempt so the script can react to failure lets you handle this gracefully.
Example: Attempting to Change a readonly Variable
#!/bin/bash
readonly LOCKED="original"
if ! (LOCKED="changed") 2>/dev/null; then
echo "As expected, LOCKED could not be reassigned"
fi
echo "LOCKED is still: $LOCKED"
Login to try C/C++/Java/PHP code in the editor
Removing a Variable with unset
unset NAME deletes a variable's definition entirely, so afterward referencing it behaves as if it was never assigned (expanding to an empty string, or triggering an error under set -u). This differs from assigning an empty string, which keeps the variable defined but empty.
Example: Removing a Variable with unset
#!/bin/bash
temp_value="scratch data"
echo "Before unset: [$temp_value]"
unset temp_value
echo "After unset: [$temp_value]"
if [ -z "${temp_value+set}" ]; then
echo "temp_value is completely unset"
fi
Login to try C/C++/Java/PHP code in the editor
Empty String vs Truly Unset
${var+set} expands to the literal word set if var is defined at all (even as an empty string), and to nothing if it is completely unset. This parameter expansion is the standard way to tell the two states apart, which a plain -z check on the value cannot do.
Example: Empty String vs Truly Unset
#!/bin/bash
empty_var=""
unset gone_var
echo "empty_var defined? ${empty_var+yes}"
echo "gone_var defined? ${gone_var+yes}"
Login to try C/C++/Java/PHP code in the editor
- Trying to change or
unsetareadonlyvariable and expecting it to silently work; Bash refuses and prints an error, and in a non-interactive script this can even cause the script to exit depending on settings. - Confusing
unset varwith setting a variable to an empty string,var=""; an empty-string variable is still set (defined) and passes-vor${var+x}tests differently than a fully unset one. - Assuming
readonlyonly prevents reassignment via=; it also blocksunseton that same variable within the same shell.
readonly NAME=valuemakes a variable's value immutable for the rest of the shell's life; attempting to reassign it produces an error.unset NAMEcompletely removes a variable's definition, distinct from setting it to an empty string.- A
readonlyvariable cannot later beunsetin the same shell session. - Both are useful for defensive scripting:
readonlyprotects constants,unsetcleans up temporary variables or ensures a fresh state.
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: