官术网_书友最值得收藏!

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 is this:

line 4: variable: readonly variable
主站蜘蛛池模板: 绍兴市| 瑞金市| 大关县| 安义县| 尚志市| 略阳县| 安乡县| 东明县| 齐齐哈尔市| 望谟县| 平潭县| 侯马市| 竹北市| 龙山县| 共和县| 大田县| 岢岚县| 德兴市| 会理县| 太仆寺旗| 龙州县| 藁城市| 邢台市| 江西省| 阿坝县| 临澧县| 华阴市| 承德县| 汾阳市| 金平| 繁峙县| 博爱县| 荆门市| 平昌县| 荥经县| 从化市| 瑞昌市| 靖江市| 视频| 鲜城| 江门市|