0

How can we check under if whether a variable is readonly or not? Please Explain with Examples

  • 2
    Does this answer your question? [Test if a variable is read-only](https://stackoverflow.com/questions/4440842/test-if-a-variable-is-read-only) – Darkhorrow Jul 01 '21 at 12:27

2 Answers2

0

You do not provide any information of what are you talking about, but I guess you want to check a variable in bash.

If that is the case, this post may help you: Test if a variable is read-only

In future questions, I suggest you to provide more information about the environment you are working on.

Darkhorrow
  • 37
  • 1
  • 9
0

If you type the command readonly without arguments, it will print the list of the readonly variables. So you can write something like this:

readonly PI=3.14
PO=3.14
if [ "$(readonly | grep -w PI)" != "" ] ; then echo read-only ; else echo not read-only ; fi
if [ "$(readonly | grep -w PO)" != "" ] ; then echo read-only ; else echo not read-only ; fi

which will result in the following output:

read-only
not read-only