Thursday, May 3, 2007

Unchanging variables: readonly


Sometime a need may arise for variables to have a constant or fixed value.
A very simple script to state the same.

$ cat readonly.sh
a=20
readonly a
echo $a
echo "Trying to set \"a\" to a diff value"
a=30
echo $a

A Sample run:
$./readonly.sh
20
Trying to set "a" to a diff value
readonly: line 6: a: readonly variable

From the shell the readonly variables can be listed using "readonly" command.

No comments:

© Jadu Saikia www.UNIXCL.com