Created
November 5, 2021 18:41
-
-
Save dathanb/716738cd685e4d9beaecc3a84108282f to your computer and use it in GitHub Desktop.
Read lines to array in Bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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