Working with read-only variables
During Shell scripting, we may need a few variables, which cannot be modified. This may be needed for security reasons. We can declare variables as read-only using following command read-only:
The usage is as follows:
$ readonly currency=Dollars
Let's try to remove the variable:
$ unset currency bash: unset: currency: cannot unset: readonly variable
If we try to change or remove the ready-only variable in the script, it will give the following error:
#!/bin/bash AUTHOR="Ganesh Naik" readonly AUTHOR AUTHOR="John"
This would produce the following result:
/bin/sh: AUTHOR: This variable is read only.
Another technique:
Declare -r variable=1 echo "variable=$variable" (( var1++ ))
Output after execution of the script:
line ...
Get Learning Linux Shell Scripting now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.