I am writing this e-mai to report to report a bug in bash. Configuration Information [Automatically generated, do not change]: Machine: x86_64 OS: linux-gnu Compiler: gcc Compilation CFLAGS: -DPROGRAM='bash' -DCONF_HOSTTYPE='x86_64' -DCONF_OSTYPE='linux-gnu' -DCONF_MACHTYPE='x86_64-unknown-linux-gnu' -DCONF_VENDOR='unknown' -DLOCALEDIR='/usr/local/share/locale' -DPACKAGE='bash' -DSHELL -DHAVE_CONFIG_H -I. -I. -I./include -I./lib -g \ -O2 uname output: Linux concour5 2.6.18-274.3.1.el5xen #1 SMP Tue Sep 6 20:57:11 EDT 2011 x86_64 x86_64 x86_64 GNU/Linux Machine Type: x86_64-unknown-linux-gnu
Bash Version: 4.2 Patch Level: 0 Release Status: release Description: Cannot redirect into an array from multiline variable Does not work: echo "$mydata" | while read line; do myarray+=( "$line" ); done Works: while read -r line; do myarray+=( "$line" ); done < <(echo "$mydata") Repeat-By: Here is exact code that shows the bug. I create a multiline variable mydata="aaa aaaa aaaaa bbb bbbb bbbbb ccc cccc ccccc" Trying to place the file into an array fails. Technique 1) echo "$mydata" | while read -r line; do myarray+=( "$line" ); done This fails. It should work but doesn't. This is a bug in bash. Fix: Technique 2) while read -r line; do myarray+=( "$line" ); done < <(echo "$mydata") This works Technique 3) Create a file foo echo "$mydata" > foo while read -r line; do myarray+=( "$line" ); done < foo This works, but requires using external files which is undesired. You can view what is in the array with this command: echo "${myarray[@]}" Please let me know if I can provide any more information about the bug. It is easily reproducible with the code I have included. For your convenience, here is a little script which summarizes the above. #! /bin/bash #First creating a multiline data file mydata="aaa aaaa aaaaa bbb bbbb bbbbb ccc cccc ccccc" # Populating the Array with mydata using technique 1 echo "Poplulating the Array using technique 1" unset myarray echo "$mydata" | while read -r line; do myarray+=( "$line" ); done echo "${myarray[@]}" # THERE IS NO OUTPUT. THIS IS A BUG WITH BASH read -n1 -r -p "Press any key to continue..." key ; echo "" # Poplulating the Array with mydata using technique 2 echo "Poplulating the Array using technique 2" unset myarray while read -r line; do myarray+=( "$line" ); done < <(echo "$mydata") echo "${myarray[@]}" # This works read -n1 -r -p "Press any key to continue..." key ; echo "" # Poplulating the Array with mydata using technique 3 echo "Poplulating the Array using technique 3" echo "$mydata" > foo unset myarray while read -r line; do myarray+=( "$line" ); done < foo rm foo echo "${myarray[@]}" # This works but requires using files, which is undesired