需求
读取如下多行文本到数组中:
abandon
ability
aboard
absence
absent
可以用如下命令读取:
WORDS=($(cat ~/custom/a.txt | xargs))
echo ${#WORDS[@]}
但是使用 IDEA shell-format 时会报如下警告:
Prefer mapfile or read -a to split command output (or quote to avoid splitting).
See SC2207.
https://github.com/koalaman/shellcheck/wiki/SC2207
推荐
mapfile -t WORDS < <(cat ~/custom/a.txt)
或者直接
mapfile -t WORDS < ~/custom/a.txt