# Strange behavior with readarray (aka mapfile)

**URL:** <https://community.unix.com/t/strange-behavior-with-readarray-aka-mapfile/326032>\
**Category:** Shell Programming and Scripting\
**Created:** [March 21, 2013, 12:26am UTC](https://community.unix.com/t/strange-behavior-with-readarray-aka-mapfile/326032 "2013-03-21T00:26:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Stora](https://community.unix.com/letter_avatar/michael_stora/32/5_5575768a8748004e209b776fc1b2916d.png) [@Michael\_Stora](https://community.unix.com/u/Michael_Stora)\
**Post date:** [March 21, 2013, 12:26am UTC](https://community.unix.com/t/strange-behavior-with-readarray-aka-mapfile/326032/1 "2013-03-21T00:26:13Z")

</div>

```nohighlight
$ readarray list <<< "a b c d e f g"
$ echo ${list[@]}
a b c d e f g

$ echo "a b c d e f g" | readarray list2
$ echo ${list2[@]}
a blank line

```

I read a post the same thing happens with cat vs \< on another forum but the poster did not know why.

Any ideas?

Mike

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [March 21, 2013, 1:15am UTC](https://community.unix.com/t/strange-behavior-with-readarray-aka-mapfile/326032/2 "2013-03-21T01:15:28Z")

</div>

> [@michael stora](#):
>
> ```plaintext
> $ readarray list <<< "a b c d e f g"
> $ echo ${list[@]}
> a b c d e f g
> 
> $ echo "a b c d e f g" | readarray list2
> $ echo ${list2[@]}
> a blank line
> 
> ```
> 
> I read a post the same thing happens with cat vs \< on another forum but the poster did not know why.
> 
> Any ideas?
> 
> Mike

When you have a pipeline, the standards allow (but do not require) the elements of the pipeline to be in different shell execution environments. If your system does put them in different shell execution environments, the readarray may be setting list2 in one shell execution environment, but not in the environment where the second echo is running. If that is your problem, the following should work:

```plaintext
echo "a b c d e f g" | ( readarray list2
echo ${list2[@]} )

```

I'm running on OS/X, which does not have a readarray command. So, I can't test it, but hopefully this will give you some hints that may help.

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/5_5575768a8748004e209b776fc1b2916d.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 21, 2013, 5:26am UTC](https://community.unix.com/t/strange-behavior-with-readarray-aka-mapfile/326032/3 "2013-03-21T05:26:09Z")

</div>

Don Cragun is right - in e.g. bash, pipes are executed in subshells, and it is difficult to get the results back to the parent shell for further processing. His proposal prints the array, yes, but that's it, it's lost for the original shell.  
If you need to process the array, you can (in bash!)

- put further processing steps into the subshell as well
- use process substitution: `$ readarray list < <(echo "a b c d e f g")`
- use array assignment and command substitution: `$ list=( $(echo "a b c d e f g") )`
- just array assignment: `$ list=( "a b c d e f g" )`

---

<div class="post-metadata">

**Author:** ![Michael\_Stora](https://community.unix.com/letter_avatar/michael_stora/32/5_5575768a8748004e209b776fc1b2916d.png) [@Michael\_Stora](https://community.unix.com/u/Michael_Stora)\
**Post date:** [March 21, 2013, 12:12pm UTC](https://community.unix.com/t/strange-behavior-with-readarray-aka-mapfile/326032/4 "2013-03-21T12:12:14Z")

</div>

Thanks to both of you for the explanation and your suggestions.

Mike
