bug-bash
[Top][All Lists]
Advanced

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

difference between "readonly" and "declare -r"


From: Cristian Zoicas
Subject: difference between "readonly" and "declare -r"
Date: Wed, 13 Oct 2010 16:26:24 +0200

---- BEGIN CONFIGURATION 1: Debian GNU/Linux 5.0 ----

Configuration Information [Automatically generated, do not change]:
Machine: i486
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i486'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i486-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOC$
uname output: Linux griffo.sissa.it 2.6.26-2-686 #1 SMP Wed Aug 19
06:06:52 UTC 2009 i686 GNU/Linux
Machine Type: i486-pc-linux-gnu

Bash Version: 3.2
Patch Level: 39
Release Status: release

---- END CONFIGURATION 1 Debian GNU/Linux 5.0 ----




---- BEGIN CONFIGURATION 2: ./configure --prefix=<dir> under Debian
GNU/Linux 5.0  ----

Configuration Information [Automatically generated, do not change]:
Machine: i686
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS:  -DPROGRAM='bash' -DCONF_HOSTTYPE='i686'
-DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='i686-pc-linux-gnu'
-DCONF_VENDOR='pc' -DLOC$
uname output: Linux griffo.sissa.it 2.6.26-2-686 #1 SMP Wed Aug 19
06:06:52 UTC 2009 i686 GNU/Linux
Machine Type: i686-pc-linux-gnu

Bash Version: 4.1
Patch Level: 9
Release Status: release

---- END  CONFIGURATION 2: ./configure --prefix=<dir> under Debian
GNU/Linux 5.0  ----



Description:

        CONFIGURATION 1: this is bash 3.2.39 you can find on Debian GNU/Linux 
5.0
        CONFIGURATION 2: this is bash 4.1.9 downloaded on a Debian GNU/Linux 5.0
                         machine and configured with
                              ./configure --prefix=<dir>

        I have tested the script below under both configurations above
        and I found differences between "readonly" and "declare -r".

        I was not able to  find something justifying this behaviour in
        the bash documentation.

        The bug which manifests on the line 77.

        Read the notes at the end.

        ---- begin script ----

           1    function tst_readonly()
           2    {
           3        # - Declare a global variable since it is not
declared "local".
           4        #
           5        RO_VAR=f
           6
           7        # - The global variable defined above becomes readonly.
           8        #
           9        readonly RO_VAR
          10    }
          11
          12    # - Call function tst_readonly to create global
variable and make it readonly
          13    #
          14    tst_readonly
          15
          16    # - Print the readonly variable contents;
          17    # - The contents is ok.
          18    #
          19    echo "RO_VAR print 1: $RO_VAR"
          20
          21
          22    # !!!!!!!!!!!!!!! Trying to assign a readonly variable
          23    #
          24    # - bash behaves correctly and prints an error message
and do not assigns
          25    #   any value to the readonly variable
          26    #
          27    RO_VAR=m
          28
          29
          30    # - The readonly variable contents is still "f" since
the above assignment failed.
          31    # - This is good.
          32    #
          33    echo "RO_VAR print 2: $RO_VAR"
          34
          35    echo
"-------------------------------------------------------------"
          36
          37    function tst_D_VAR_1()
          38    {
          39        # - Declare a global variable since it is not
declared "local".
          40        #
          41        D_VAR_1=d_var_1_first_assignment
          42
          43        # - The global variable defined above becomes readonly.
          44        #
          45        declare -r D_VAR_1
          46
          47
          48        # !!!!!!!!!!!!!!! Trying to assign a readonly variable
          49        #
          50        # - bash behaves correctly and prints an error
message and do not assigns
          51        #   any value to the readonly variable
          52        #
          53        D_VAR_1=d_var_1_second_assignment
          54
          55
          56        # - The readonly variable contents is still
"d_var_1_first_assignment" since the above assignment failed.
          57        # - This is good.
          58        #
          59        # This following statement is not executed  (isn't
this another bug?)
          60        #
          61        echo "D_VAR_1 print 1:  $D_VAR_1"
          62
          63    }
          64
          65    # - Call function to create a readonly global variable
          66    #
          67    tst_D_VAR_1
          68
          69    # - print variable contents
          70    # - The contents is ok; the second assignment in the
function did not work
          71    #
          72    echo "D_VAR_1 print 2:  $D_VAR_1"
          73
          74    # - assign the global readonly variable
          75    # - BUG : the assignment succeeds !!!!!!
          76    #
          77    D_VAR_1=d_var_1_third_assignment
          78
          79    echo "D_VAR_1 print 3:  $D_VAR_1"

        ---- end script ----

        a) "readonly" and "declare -r" do not behave the same way;

        b) "declare -r" makes the  variable readonly but only in a
           function and this is not correct.


Repeat-By:
        [Describe the sequence of events that causes the problem to occur.]

Fix:

        1) In  order to  solve point  b the  bash behaviour  should be
           corrected  since "declare  -r"  is not  able  to provide  a
           variable with the "readonly" attribute.

        2) I find the behaviour of "readonly" correct since it is able
           to give the readonly attribute to an existing variable. Nothing
           to be done here.



regards
cristian zoicas



reply via email to

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