Skip to content

Instantly share code, notes, and snippets.

@dathanb
Created November 5, 2021 18:41
Show Gist options
  • Save dathanb/716738cd685e4d9beaecc3a84108282f to your computer and use it in GitHub Desktop.
Save dathanb/716738cd685e4d9beaecc3a84108282f to your computer and use it in GitHub Desktop.
Read lines to array in Bash
# Mac OS ships with Bash 3.2, which lacks the read -a option, which makes reading into an array easy
# So this is a replacement
lines="first line
second line
third line"
linesArray=()
while read -r line; do
linesArray+=("$line")
done <<< "${lines}"
# verify the array is initialized correctly
echo "${#linesArray}"
echo "${linesArray[@]}"
for line in "${lines}"; do
echo "line: ${line}"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment