bug-bash
[Top][All Lists]
Advanced

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

Re: Assignment to RO variable


From: Dennis Williamson
Subject: Re: Assignment to RO variable
Date: Tue, 15 Aug 2023 23:24:31 -0500

On Tue, Aug 15, 2023 at 8:57 PM Wiley Young <wyeth2485@gmail.com> wrote:

> 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: $?"
>
> ###
>

You have assigned the character "y" to the variable "x" which you've set to
read only using the readonly built. It is forever and always immutable (or
the shell it exists in exits).

>From man bash:

readonly [-aAf] [-p] [name[=word] ...]
              The given names are marked readonly; the values of these
names may not be changed by subsequent assignment.  If the -f option is
supplied, the functions
              corresponding to the names are so marked.  The -a option
restricts the variables to indexed arrays; the -A option restricts the
variables to associative arrays.  If
              both options are supplied, -A takes precedence.  If no name
arguments are given, or if the -p option is supplied, a list of all
readonly names is printed.  The
              other options may be used to restrict the output to a subset
of the set of readonly names.  The -p option causes output to be displayed
in a format that may be
              reused as input.  If a variable name is followed by =word,
the value of the variable is set to word.  The return status is 0 unless an
invalid option is
              encountered, one of the names is not a valid shell variable
name, or -f is supplied with a name that is not a function.

Your subsequent attempts to change the value of x will result in readonly
variable errors with a return status is 1 which will affect any logic
making use of it.

Don't use readonly unless you really mean it.

You should use more quotes around variables.

b "$1"
echo "$1"

Your use of colon as a comment character is confusing.

-- 
Visit serverfault.com to get your system administration questions answered.


reply via email to

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