mapfile

noun

say it: “MAP-fyl” /ˈmæpˌfaɪl/

A bash command that reads lines of text and puts each one in the next slot of an array.

What does mapfile do in bash?

mapfile reads its input and puts each line in the next slot of an array, so a file with one song a line becomes a list of songs. -t cuts off each line's newline. It needs bash 4 or newer, so a Mac's own bash 3.2 doesn't have it.

mapfile -t songs < songs.txt
echo "${#songs[@]} songs"   # 6 songs
echo "${songs[3]}"          # Gas Station Sunrise

In a sentence

  • “Without -t, mapfile leaves a hidden line break at the end of every title.”
  • “mapfile -t filled the songs array from songs.txt, one title a slot.”

Level: Journeyman · More Unix terms at this level

Updated