Skip to Content
Learning Linux Shell Scripting
book

Learning Linux Shell Scripting

by Ganesh Sanjiv Naik
December 2015
Beginner
306 pages
5h 2m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting

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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Learning Linux Shell Scripting - Second Edition

Learning Linux Shell Scripting - Second Edition

Ganesh Sanjiv Naik

Publisher Resources

ISBN: 9781785286216