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 attributedeclare -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=$PATHdeclare -n ref
ref=/usr/bindeclare -p /opt/bin  # declare -- /opt/bin="/usr/bin"
declare -n PATH      # declare -nr PATH="/opt/bin"echo $PATH
# /usr/bin

​

Reply via email to