bug-bash
[Top][All Lists]
Advanced

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

Re: @K transformation


From: Léa Gris
Subject: Re: @K transformation
Date: Sat, 21 Aug 2021 02:48:00 +0200
User-agent: Telnet/1.0 [tlh] (PDP11/DEC)

Le 21/08/2021 à 00:59, Greg Wooledge écrivait :
The fact that "${a[@]@K}" expands to a single word is surprising to me.
I know someone else already mentioned it in this thread (sorry, I forgot
who it was), but it would be nice if there were a similar one that gave
a list of multiple words.

unicorn:~$ printf '<%s> ' "${a[@]@Q}"; echo
<'1'> <'2'> <'3'>
unicorn:~$ printf '<%s> ' "${a[@]@U}"; echo
<1> <2> <3>
unicorn:~$ printf '<%s> ' "${a[@]@L}"; echo
<1> <2> <3>
unicorn:~$ printf '<%s> ' "${a[@]@E}"; echo
<1> <2> <3>
unicorn:~$ printf '<%s> ' "${a[@]@K}"; echo
<0 "1" 1 "2" 2 "3">

It really sticks out.


Not all expansion transformers returns same number of arguments when expanding an array

Lets test with this script:

#!/usr/bin/env bash

oper=(U u L Q E P A K a)

arr=(foo bar baz qux)

mapfile -t expansions < <(
  printf '"${arr[@]@%s}"\n' "${oper[@]}"
)

for exp in "${expansions[@]}"; do
  eval "set -- $exp"
  if [ $# -gt 1 ]; then
    printf '%s expands into %d argumsnts:\n' "$exp" "$#"
    for ((i = 1; $#; i++)); do
      printf '%d.\t%s\n' $i "$1"
      shift
    done
  else
    printf '%s expands into a single argument:\n1.\t%s\n' "$exp" "$1"
  fi
  printf \\n
done

The output is:

"${arr[@]@U}" expands into 4 argumsnts:
1.      FOO
2.      BAR
3.      BAZ
4.      QUX

"${arr[@]@u}" expands into 4 argumsnts:
1.      Foo
2.      Bar
3.      Baz
4.      Qux

"${arr[@]@L}" expands into 4 argumsnts:
1.      foo
2.      bar
3.      baz
4.      qux

"${arr[@]@Q}" expands into 4 argumsnts:
1.      'foo'
2.      'bar'
3.      'baz'
4.      'qux'

"${arr[@]@E}" expands into 4 argumsnts:
1.      foo
2.      bar
3.      baz
4.      qux

"${arr[@]@P}" expands into 4 argumsnts:
1.      foo
2.      bar
3.      baz
4.      qux

"${arr[@]@A}" expands into 3 argumsnts:
1.      declare
2.      -a
3.      arr=([0]="foo" [1]="bar" [2]="baz" [3]="qux")

"${arr[@]@K}" expands into a single argument:
1.      0 "foo" 1 "bar" 2 "baz" 3 "qux"

"${arr[@]@a}" expands into 4 argumsnts:
1.      a
2.      a
3.      a
4.      a


Now @K would have been useful if it expanded into individual arguments for each entry rather than an eval expression.

The @A suffers from the same weirdness expanding into an eval expression that actually duplicates the feature from declare -p.

The @a expands the attribute from the container array for each element, which is as strange, because all array elements don't have attributes of their own.

--
Léa Gris




reply via email to

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