On 27/06/2023 21:05, Kerin Millar wrote:
It doesn't work at all for >=5.2. The reason for this is interesting and I may
make a separate post about it.
Prior to 5.2, it can easily be tricked into printing names that do not exist.
$ VAR=$'\nNONEXISTENT=' ./declare-P | grep ^NONEXISTENT
NONEXISTENT
Thank you. I was just reading the discussion in Gentoo forum and
realizing that I've been to quickly: it doesn't pass the
FOO=$'\nBAR BAZ QUUX=' test. But what about this? (I also quickly
tested with Bash 5.2.15).
FOO=$'\nBAR BAZ QUUX='
VAR=$'\nNONEXISTENT='
declare-P() {
local curVar
declare -a curVars
readarray -t curVars <<<"$1"
curVars=( "${curVars[@]%%=*}" )
curVars=( "${curVars[@]##* }" )
for curVar in "${curVars[@]}"; do
### we can use [[ -v "$curVar" ]] at some point!
[[ "${curVar//[a-zA-Z0-9_]}" || \
"${curVar:0:1}" == [0-9] || \
! -v "$curVar" || \
! "$curVar" =~ $2 ]] || printf '%s\n' "$curVar"
done
}
declare-P "$(declare -p)"
echo "##################"
declare-P "$(declare -p)" "QU|^NON|VAR"