bash: read File into Array

2019-01-13 00:00:00 IN1 , 2023-04-10 14:16:30 IN1


Q: how to read a file into array with bash?

A: use mapfile

example


// file to read
sMimeTypeList="/etc/mime.types";

// create array `aMimeType`
mapfile aMimeType < <(cat "$sMimeTypeList" | grep -v "^$");

// test outputs
echo "${aMimeType[@]}";         # all
echo "100: ${aMimeType[100]}";  # just the 100. entry
This website uses Cookies to provide you with the best possible service. Please see our Privacy Policy for more information. Click the check box below to accept cookies. Then confirm with a click on "Save".