bug-bash
[Top][All Lists]
Advanced

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

Re: Declaring arrays with empty string in one line is bugged


From: Koichi Murase
Subject: Re: Declaring arrays with empty string in one line is bugged
Date: Wed, 5 Aug 2020 03:55:26 +0900

It also reproduces in the current devel branch.

$ bash-dev -c 'declare -a arr=(""); declare -p arr'
declare -a arr=()
$ bash-5.0 -c 'declare -a arr=(""); declare -p arr'
declare -a arr=([0]="")

2020-08-05 3:33 andy_bash <andy_bash@objectmail.com>:
> Sorry, the colors got messed up on the last email

It's still messed up. I reformatted:

> Configuration Information [Automatically generated, do not change]:
> Machine: x86_64OS: linux-musl
> Compiler: gcc
> Compilation CFLAGS: -g -O2 -Wno-parentheses -Wno-format-security
> uname output: Linux 37b7613044c1 5..5.7-200.fc31.x86_64 #1 SMP Fri Feb
> 28 17:18:37 UTC 2020 x86_64 GNU/Linux
> Machine Type: x86_64-pc-linux-musl
> Bash Version: 5.1Patch Level: 0
> Release Status: alpha
>
> Description:
>
>     Declaring an array and setting it with empty strings ("") in one
>     line is bugged in bash 5.1 alpha. This has worked in bash
>     3.2-5.0 without issue.
>
>     I first discovered this through a piece of indirection code I
>     have:
>
>     x=("")
>     y="x[@]"
>     local z=(${!y+"${!y}"})
>
> Repeat-By:
>
>     docker run -it --rm bash:5.1-alpha bash -c '
>       function foo(){
>         local bug=("" "5" "" 1 "");
>         declare -a bug2=("");
>         declare -ga bug3=("" "5" "" 1 "");
>         local not_bug=("no" "nulls");
>         local workaround; workaround=("");
>         declare -p bug bug2 bug3 not_bug workaround;
>       };
>       declare -a bug4=("" "5" "" 1 ""); declare -p bug4; foo'
>
>    Results:
>     declare -a bug4=([0]="5" [1]="1")
>     declare -a bug=([0]="5" [1]="1")
>     declare -a bug2=()
>     declare -a bug3=([0]="5" [1]="1")
>     declare -a not_bug=([0]="no" [1]="nulls")
>     declare -a workaround=([0]="")
>
>     As you can see, all the empty strings (null strings) are
>     missing, except in the workaround
>
> Fix:
>
>     Declaring an array with empty strings ("") in one line should
>     work, just like it did in bash 3.2-5.0
>
>     This original piece of code should also work the same as
>     versions 3.2-5.0 too
>
>     $ docker run -it --rm bash:5.0 bash -euc '
>     > function foo(){
>     >   local name="${1}[@]";
>     >   local copy=(${!name+"${!name}"});
>     >   declare -p copy;
>     > };
>     > x=("" 5 ""); foo x; foo undeclared_x'
>     declare -a copy=([0]="" [1]="5" [2]="")
>     declare -a copy=()
>     $ docker run -it --rm bash:5.1-alpha bash -euc '
>     > function foo(){
>     >   local name="${1}[@]";
>     >   local copy=(${!name+"${!name}"});
>     >   declare -p copy;
>     > };
>     > x=("" 5 ""); foo x; foo undeclared_x'
>     declare -a copy=([0]="5")
>     declare -a copy=()
>
> Thanks,-Andy

--
Koichi



reply via email to

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