Bash: 入力された内容を行単位で配列に格納する
readarray は互換性が不十分なので使いたくない。
IFS を正しく扱うのがポイント。
ワンライナー
IFS=$'\n' lines=($(cat))
for ループ
declare -a lines
while IFS= read line; do
lines+=("$line")
done
実行例
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 件のコメント:
コメントを投稿