bug-bash
[Top][All Lists]
Advanced

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

Assignment to RO variable


From: Wiley Young
Subject: Assignment to RO variable
Date: Tue, 15 Aug 2023 18:57:11 -0700

So this behavior of `bash`s seems like a bug to me. I stumbled across it by
accident.

>From within a stack of x3 functions called from a case within a for loop,
when trying to assign a value to a read-only variable, `printf` and `read`
both fail with exit codes of 1, and the functions, `case` and `for`
commands all complete.  When trying effectively the same thing with a
simple assignment syntax, `x=z`, when the assignment fails, the functions
return, `case` fails to complete and `for` returns an exit code of 1.

It's probably a pretty rare use case, though. I tried this out on Android
12 on a Nokia in Termux and Fedora 38 on an HP in XFCE; that's all I've got
for the time being. There isn't any pressing need to look into this one;
I'm just curious. A reproducer's attached.

Thanks & Happy summer,
Wiley

###
$ cat ro-var-in-fn.sh
#!/bin/bash -x

reset
readonly x=y
declare -p x SHELLOPTS BASHOPTS
: "hyphen: $-"

a(){ : go a; b $1; : end a;}
b(){ : go b; c $1; : end b;}
c(){
  : go c
  declare -p x

  case "$1" in
    1 )
      false
      : "exit, false: $?"
      ;;
    2 )
      printf -v x '%s' $1
      : "exit, printf: $?"
      ;;
    3 )
      read -r x <<< $1
      : "exit, read: $?"
      ;;
    4 )
      x=$1
      : "exit, var assignment: $?"
      ;;
    5 )
      echo $1
      ;;
  esac
  : "exit, case: $?"
  : end c
}

declare -pf a b c

for ii in {1..5}
do
  a $ii
  : "exit, a $ii: $?"
done
: "exit, for loop: $?"

###


reply via email to

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