bug-bash
[Top][All Lists]
Advanced

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

[PATCH] Skip initial expansion of valid array reference tokens in unset


From: konsolebox
Subject: [PATCH] Skip initial expansion of valid array reference tokens in unset
Date: Mon, 19 Apr 2021 23:09:01 +0800

Attached patch demonstrates a solution that solves the current issues
of unset discussed in
https://lists.gnu.org/archive/html/bug-bash/2021-03/msg00056.html
while avoiding breakage of scripts and keeping the expansion of
subscripts consistent with expansion in references.

The patched code is also available in
https://github.com/konsolebox/bash/tree/skip_expansion_of_valid_array_refs_in_unset.
The patch is applied against the latest commit (f3a35a2d60) in master
branch.  The changes can be conveniently compared in
https://git.io/JOgrE.

The solution allows the following tests to pass.  A few of them were
described to fail in the other thread.

-------------------------

#!/bin/bash

declare -A a
key='$(echo foo)'

# Here the tokens are valid array references and are not expanded.
# The references are completely similar to the assignments.
# This solves the surprise expansion issues.

a[$key]=1
unset a[$key]
declare -p a # Displays no element

a['$key']=2
unset a['$key']
declare -p a # Displays no element

a["foo"]=3
unset a["foo"]
declare -p a # Displays no element

echo -----

# Here the tokens are "strings".  They expand and keep the
# original behavior and allows existing scripts to not break.
# It also allows nref or iref references to be transparently
# referenced in it.

a[$key]=1
unset 'a[$key]' # Transforms to a[$key] after expansion
declare -p a # Displays no element

a['$key']=2
unset "a['\$key']" # Transforms to a['$key'] after expansion
declare -p a # Displays no element

a["foo"]=3
unset 'a["foo"]' # Transforms to a["foo"] after expansion
declare -p a # Displays no element

echo -----

# The update also keeps compatibility with already existing behavior of
# unset when assoc_expand_once is enabled, but only for quoted tokens.
# I would prefer it totally removed though.
# Also notice assoc_expand_once's limitation below.

shopt -s assoc_expand_once

a[$key]=1
unset "a[$key]"
declare -p a # Displays no element

a['$key']=2
unset "a[\$key]"
declare -p a # Displays no element

a["foo"]=3
unset "a[foo]"
declare -p a # Displays no element

echo ----------

# For unsetting '@' and all elements:

key=@

declare -A a=(@ v0 . v1)
unset a[$key]
declare -p a # Displays 'declare -A a=([.]="v1" )'

declare -A a=(@ v0 . v1)
unset a[@]
declare -p a # Displays 'declare: a: not found'

echo -----

shopt -u assoc_expand_once

declare -A a=(@ v0 . v1)
unset 'a[$key]'
declare -p a # Displays 'declare -A a=([.]="v1" )'

declare -A a=(@ v0 . v1)
unset 'a[@]'
declare -p a # Displays 'declare: a: not found'

echo -----

shopt -s assoc_expand_once

declare -A a=(@ v0 . v1)
unset "a[$key]"
declare -p a # Displays 'declare: a: not found' (A limitation)

declare -A a=(@ v0 . v1)
unset 'a[@]'
declare -p a # Displays 'declare: a: not found'


-- 
konsolebox

Attachment: skip-expansion-of-valid-array-refs-in-unset.patch
Description: Source code patch


reply via email to

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