readonly Variables
In this page:
readonly Variables
Marking a variable readonly prevents it from being reassigned later in the script, causing an error if something tries to change it. This is useful for constants that should never be accidentally overwritten. Once set readonly, a variable stays that way for the rest of the script's execution.
Warning: Attempting to reassign a readonly variable does not silently fail — it prints an error to stderr.
Example: readonly Variables
#!/bin/bash
readonly PI=3.14159
echo "Pi is $PI"
PI=3.14 # this will produce an error
echo "This line may not run depending on the shell"
Login to try C/C++/Java/PHP code in the editor
🔒
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: