[Top][All Lists]

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

Re: cannot declare local variables if they're readonly

From: isabella parakiss
Subject: Re: cannot declare local variables if they're readonly
Date: Thu, 23 Jul 2015 16:57:42 +0200

On 7/23/15, Greg Wooledge <address@hidden> wrote:
> People who use "readonly" are generally doing so in the context of a
> "restricted shell" (yes, commence laughter) or other situation where
> that specific variable is the key to unlocking something that the
> administrator does not want the user to unlock.  The entity who used
> "readonly" is presumed to want that variable to remain unchanged, forever.

On 7/23/15, Stephane Chazelas <address@hidden> wrote:
> 2015-07-23 01:12:01 +0200, isabella parakiss:
> What that means is that with this kind of dynamic scoping,
> "readonly" is not very helpful. I don't remember ever using it.

You guys are assuming that I am the one who uses readonly.
I'm not, *bash* is.

The fact is, I found out this by using BASH_REMATCH, trying to use it in
different functions without interfering with each other.
And it did work, sometimes.  I couldn't even reproduce it in a new shell.

It turns out that bash-completion uses it.  And why wouldn't they?
So my functions stopped working because I tried to... tab complete strace?
WOW.  This makes so much sense.

I imagine that people want to be able to use something like this:

extract_package () {
  if [[ $1 =~ (.*)-([0-9]+\.[0-9]+)\.tar.gz ]]; then
    tar xf "$1"
    if install_package; then
      echo "${BASH_REMATCH[1]} ${BASH_REMATCH[2]} successfully installed"
      echo "Couldn't install ${BASH_REMATCH[1]} ${BASH_REMATCH[2]}"

install_package () {
  some legit use of [[ =~ ]]

Anyway for some reason that I don't fully understand, the above won't even
work when BASH_REMATCH is not defined, as shown below.

$ declare -p BASH_REMATCH
bash: declare: BASH_REMATCH: not found
$ f () { local BASH_REMATCH; [[ abc =~ .(.*). ]]; g; declare -p BASH_REMATCH; }
$ g () { local BASH_REMATCH; [[ xyz =~ .(.*). ]]; }
$ f
bash: declare: BASH_REMATCH: not found
$ f
bash: local: BASH_REMATCH: readonly variable
bash: local: BASH_REMATCH: readonly variable
declare -ar BASH_REMATCH='([0]="xyz")'

To sum it up: for some reason that you can't control, local variables may
or may not be assigned correctly.  Also don't use local BASH_REMATCH
because it doesn't work.
Bash is a very usable language and this is totally not a problem.

xoxo iza

reply via email to

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