Skip to Content
Learning Linux Shell Scripting - Second Edition
book

Learning Linux Shell Scripting - Second Edition

by Ganesh Sanjiv Naik
May 2018
Beginner
332 pages
7h 28m
English
Packt Publishing
Content preview from Learning Linux Shell Scripting - Second Edition

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 by using the following read-only command:

$ readonly currency=Dollars

Let's try to remove the variable:

$ unset currencybash: unset: currency: cannot unset: readonly variable

If we try to change or remove the read-only variable in the script, it will give the following error:

#!/bin/bash 
AUTHOR="Ganesh Naik" 
readonly AUTHOR 
AUTHOR="John" 

This will produce the following result:

/bin/sh: AUTHOR: This variable is read only.

Another technique is as follows:

declare  -r  variable=1 
echo "variable=$variable" 
(( var1++ )) 

The output after execution of the script ...

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

Learning Linux Shell Scripting

Ganesh Sanjiv Naik
Linux Shell Scripting Cookbook - Third Edition

Linux Shell Scripting Cookbook - Third Edition

Clif Flynt, Sarath Lakshman, Shantanu Tushar

Publisher Resources

ISBN: 9781788993197Supplemental Content