bug-bash
[Top][All Lists]
Advanced

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

Data piped to 'mapfile', 'readarray', 'read -a' shell builtins is ignore


From: Rob Robason
Subject: Data piped to 'mapfile', 'readarray', 'read -a' shell builtins is ignored
Date: Tue, 23 Mar 2010 22:43:02 +0000

Configuration: 
    OS: Fedora 11 - with all updates applied
    Bash: version 4.0.23(1)-release

Problem description:
When stdin is a pipeline to 'mapfile', 'readarray', and 'read -a' shell
built-in commands, they fail to assign any values to the target array.
The array is neither unset nor altered in any way. No error message is
produced, and $? == 0.
None of the available options have any effect on this behavior. 
Oddly, when input is provided via keyboard or redirection (<), these
commands function properly. This last point seems really weird to me and
points to the built-ins' or shell's internal handling of a pipeline.

mapfile/readarray example (mapfile behaves identically to readarray):
    $ ls -a | readarray            # using the default MAPFILE array
    $ echo $?
    0
    $ echo "${#MAPFILE[@]}"
    0
    $

Other forms of mapfile/readarray that fail:
    $ ls -a | readarray -u0        # specified input file descriptor
    $ echo "${#MAPFILE[@]}"
    0
    $

    $ declare -a myArray           # (optional) Declaring myArray has no
effect     
    $ ls -a | readarray myArray
    $ echo "${#myArray[@]}"
    0
    $

read -a example:
    $ ls -a | read -a myArray
    $ echo "${#myArray[@]}"
    0
    $ 

The other ways of using stdin (i.e. keyboard, file redirection) work
fine. For example:

mapfile/readarray keyboard:
    $ readarray myArray    # input from keyboard
    1
    line 2
    3
    [cntl-D]
    $ echo "${#myArray[@]}"
    3
    $ 

mapfile/readarray redirection:
    $ ls -a > foo
    $ readarray myArray < foo
    $ echo "${#myArray[@]}"
    6
    $ 

read -a redirection: 

    $ ls -a > foo
    $ read -a myArray < foo
    $ echo "${#myArray[@]}"
    1
    $ 

Rob Robason
rob@robason.net
925-825-1512


reply via email to

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