Bash: 入力された内容を行単位で配列に格納する
readarray は互換性が不十分なので使いたくない。
IFS を正しく扱うのがポイント。
ワンライナー
1 | IFS=$'\n' lines=($(cat)) |
for ループ
1 2 3 4 | declare -a lines while IFS= read line; do lines+=("$line") done |
実行例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | bash-3.2$ IFS=$'\n' lines=($(cat)) a b c d e f g bash-3.2$ printf '[%s]\n' "${lines[@]}" [ a b c] [d] [e ] [ f ] [g ] bash-3.2$ lines=() bash-3.2$ while IFS= read line; do lines+=("$line"); done a b c d e f g bash-3.2$ printf '[%s]\n' "${lines[@]}" [ a b c] [d] [e ] [ f ] [] [g ] |
0 件のコメント:
コメントを投稿