wait, this one is all right, look: 1. you are making a readonly variable: readonly USER=sandbox 2. then you are creating a nameref: declare -n USER; from now on the assignments to USER are assignments to variable of name sandbox 3. then you create a variable sandbox and assign a value
since sandbox variable is not read only, then assignments are allowed. if you do readonly sandbox=someuser you will get errors all around. USER variable value is untouched (looking at the code); the find_varibale calls return the sandbox entry, as they should. pg On 27 Apr 2016, at 14:41, Grisha Levit wrote: > On Wed, Apr 27, 2016 at 7:37 AM, Piotr Grzybowski <narsil...@gmail.com> wrote: > > > > It seems to me that creating the reference should be allowed, but the access > to the referenced variable should honor its attributes. > > > Once you convert the variable to a reference, you can control its value by > modifying the value of a different variable with the name that corresponds to > the value of the readonly variable, so “access to the referenced variable > should honor its attributes” probably won’t do much (If I understand your > suggestion correctly). > > In a less convoluted example that doesn’t rely on creating our own namerefs: > > readonly > USER=sandbox > > USER=root > # bash: USER: readonly variable > > > > declare > -n USER > sandbox=root > # works > > USER=root > # works > > > [[ > $USER == root ]] # 0 > > > > # USER is unchanged, other than the -n attribute > declare -p USER # declare -nrx USER="sandbox" > The above works when the readonly variable has a value that is also a valid > identifier. In my previous example I worked around this using the fact that > ref=<whatever>; declare -n ref does not check to make sure that $ref is a > valid identifier. > > So: > > readonly > PATH=/opt/bin > > ref= > $PATH > declare > -n ref > ref=/usr/bin > > declare -p /opt/bin # declare -- /opt/bin="/usr/bin" > > > > declare -n PATH # declare -nr PATH="/opt/bin" > echo $PATH # /usr/bin