bug-bash
[Top][All Lists]
Advanced

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

Re: param expansion with single-character special vars in the environmen


From: Grisha Levit
Subject: Re: param expansion with single-character special vars in the environment
Date: Wed, 27 Apr 2016 08:41:20 -0400

On Wed, Apr 27, 2016 at 7:37 AM, Piotr Grzybowski <narsil.pl@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

reply via email to

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