Sunday, April 19, 2026

Read-Only Variables in Bash

 

Bash has a little known concept of read-only variables.  When specifying 'readonly' during a variable assignment prevents the variable from being reassigned.

 For example;

 1  #!/bin/bash

2

3  readonly x=0

4  echo $x

5  x=1

6  echo $x

Line 3 specifies a read-only assignment of variable x to 0, an attempt to reassign on line 5 will be disregarded and produce an error message.  The error doesn't prevent continuation of the script however.

 

::::::::::::::

No comments:

Post a Comment