help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use readonly wherever possible?


From: Greg Wooledge
Subject: Re: Use readonly wherever possible?
Date: Wed, 6 May 2020 08:35:48 -0400
User-agent: Mutt/1.10.1 (2018-07-13)

On Wed, May 06, 2020 at 10:44:08AM +0200, D630 wrote:
> Recently, I had to review one of my team members shell code. She is a Java
> developer, and used to declare variables with the final keyword. So in shell
> she was constanly using the readonly attribute like:
> 
> foo() {
>       local -r bar=$1
>       local -r lich=$2
>       echo "$((bar * lich))"
> }
> 
> I think that's unusual code in the shell-code universum. My first reflex was
> to say "No, don't do that", but I hesitate with my answer, because I have no
> idea what happens exactly in bash when a variable is declared readonly. So
> is there any advantage in using readonly wherever possible?

Go with your instinct.  Readonly is bad news in bash.  It's not designed
to be a temporary shawl that you put over a variable.  It's designed to
be permanent.

We just had a similar discussion on bug-bash:

https://lists.gnu.org/archive/html/bug-bash/2020-04/msg00200.html



reply via email to

[Prev in Thread] Current Thread [Next in Thread]