How to make a Variable read-only (constant) in Bash
Quote from HeelpBook on February 6, 2021, 4:13 pmI'm writing a Bash script on a Linux system to automize some boring stuff that needs to be executed regularly. My need now is to create a read-only variable in it. How to make a read-only variable in a Bash script?
I'm writing a Bash script on a Linux system to automize some boring stuff that needs to be executed regularly. My need now is to create a read-only variable in it. How to make a read-only variable in a Bash script?
Quote from HeelpBook on February 6, 2021, 5:09 pmIn Bash a variable can be made read-only with the following syntax:
readonly var[=value]
The square brackets around =value mean that the assignment of a value is not always necessary. For instance, if the variable had previously been created and assigned a value, and we want to make it read-only (and not change its current value), we will not need to use =value.
If the variable did not previously exist, and wemake it read-only, you may never assign a value to the variable. Take note that the value of a read-only variable cannot be changed. This is why read-only variables are referred to as constants:
$ var=constant
$ readonly var
$ unset var
var: is read only
$ var=new_value
var: is read onlyA very similar approach is used also in another command line shell called Korn Shell (developed by IBM for AIX systems) in which we have an additional way to declare a variable as read-only (the above one is valid also in Korn Shell or ksh):
A variable can be made read-only by using either of the following syntaxes:
typeset -r var[=value]
Or, as we have already seen in the above example:
readonly var[=value]
The following is a Korn shell example:
$ ksh
$ typeset -r cvar=constant
$ unset cvar
ksh: cvar: is read only
$ cvar=new_value
ksh: cvar: is read only
In Bash a variable can be made read-only with the following syntax:
readonly var[=value]
The square brackets around =value mean that the assignment of a value is not always necessary. For instance, if the variable had previously been created and assigned a value, and we want to make it read-only (and not change its current value), we will not need to use =value.
If the variable did not previously exist, and wemake it read-only, you may never assign a value to the variable. Take note that the value of a read-only variable cannot be changed. This is why read-only variables are referred to as constants:
$ var=constant
$ readonly var
$ unset var
var: is read only
$ var=new_value
var: is read only
A very similar approach is used also in another command line shell called Korn Shell (developed by IBM for AIX systems) in which we have an additional way to declare a variable as read-only (the above one is valid also in Korn Shell or ksh):
A variable can be made read-only by using either of the following syntaxes:
typeset -r var[=value]
Or, as we have already seen in the above example:
readonly var[=value]
The following is a Korn shell example:
$ ksh
$ typeset -r cvar=constant
$ unset cvar
ksh: cvar: is read only
$ cvar=new_value
ksh: cvar: is read only