readonly arguments in a script

The read command is used in scripting to accept an input from the user and store it in a variable. The value assigned to the variable read using the command read can be changed any number of times in the script.

If we want to have a variable whose value remains fixed through out the script and which can not be modified by any assignments once a value is assigned to it, we can make use of command "readonly".

For example in the script below we are reading the variable "var", as readonly and then try to change the value by assigning a new value to it.

Save it,give execute permissions and execute the script.

When the script reaches the fourth line, it throws an error

Clearly indicating that we are not allowed the change the value of "var".

No comments:

Post a Comment

Follow by Email