I just discovered mapfile in bash when shellcheck recommended it to my code to capture two shell parameters from a space-delimited string of two words. But, mapfile does not seem to function consistently. For example, this works as expected:
mapfile arr < myfile
The array arr is populated with entries corresponding to the lines of myfile. However, this does not work:
echo -e "hello\nworld" | mapfile arr
The array arr is not populated at all. And, this doesn't work either:
echo "hello world" | mapfile -d ' ' arr
I am not sure why it would make a difference where the standard input for the command comes from. I didn't know it would be possible to distinguish what the input came from, a file or a pipeline.
Any clues?
Note to moderator: It was suggested my question was a duplicate to Read values into a shell variable from a pipe . I do not agree. Nowhere is mapfile mentioned in that question, nor was there any other useful Q/A found in a SO search. In addition, that referenced question did not deal with shell parameter assignments. Therefore, this question and answers are valuable.