Bash Read a file (data stream, variable) line-by-line (and/or field-by-field)? Read fields of a string into an array


Example

Let's assume that the field separator is :

var='1:2:3:4:
newline'
arr=()
while IFS= read -d : -r field || [ -n "$field" ]; do
   arr+=("$field")
done <<< "$var"
echo "${arr[4]}"

Output:

newline