Configuration Information [Automatically generated, do not change]:
Machine: x86_64
OS: linux-gnu
Compiler: gcc
Compilation CFLAGS: -g -O2 -flto=auto -ffat-lto-objects -flto=auto
-ffat-lto-objects -fstack-protector-strong -Wformat -Werror=format-security
-Wall
uname output: Linux maggid 6.5.0-35-generic #35~22.04.1-Ubuntu SMP
PREEMPT_DYNAMIC Tue May  7 09:00:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
Machine Type: x86_64-pc-linux-gnu

Bash Version: 5.1
Patch Level: 16
Release Status: release


Description:
        When using space or newline as a delimiter with readarray -d,

        elements in the array have the delimiter replaced with NULL,

        which is left embedded in each element of the array. This
        causes incorrect behavior when using array elements as arguments to
sub-processes.
        Using other delimiters, like comma, slash, has no problem.
        Also using -t to remove the trailing delimiter is ok.
        I have reproduced this problem with the latest bash version 5.3
alpha.

Repeat-By:
        I first noticed the problem when trying to use an array element as
part of an
        argument to sed:
                readarray -d ' ' x << "A B"
                sed -e s/X/${x[0]}/
        This caused sed to complain "unterminated `s' command".
        Using "read -a" instead of readarray produces correct results.
        It does not appear to matter where the input for readarray comes
from,
        be it <, <<, or <<<. The actual delimiter also does not appear
        relevant.
        With a simple C program to print out the characters in argv[1], one
        can see that a NULL character is left in the argument. Program:
#include <stdio.h>
#include <string.h>
void main(int argc, char *argv[])
{
        int i, n;
        if (argc > 1) {
                n = strlen(argv[1]);
                for (i=0; i<n+2; i++) printf("%d ", argv[1][i]);
        }
}
$ readarray -d ' ' X <<< "A B C"
$ read -d ' ' -a   Y <<< "A B C"
$ readarray -td ' ' Z <<< "A B C"
$ ./printarg ${X[0]}A
65 0 65 $
$ ./printarg ${Y[0]}A
65 65 0 83 $
$ ./printarg ${Z[0]}A
65 65 0 83 $

Reply via email to