On Wed, Mar 24, 2010 at 12:43 AM, Rob Robason <r...@robason.net> wrote:
> 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 "${#mapfi...@]}" > 0 > $ > > Other forms of mapfile/readarray that fail: > $ ls -a | readarray -u0 # specified input file descriptor > $ echo "${#mapfi...@]}" > 0 > $ > > $ declare -a myArray # (optional) Declaring myArray has no > effect > $ ls -a | readarray myArray > $ echo "${#myarr...@]}" > 0 > $ > a It's a faq, commands in a pipe are executed in a subshell and don't modify the parent shell.