Недавний bash поставляется с mapfile
или readarray
:
readarray stuff < <(ispell -l < "$1")
echo "number of lines: ${#stuff[@]}"
( этот пример эффективно возвращает ispell -l < "$1"|wc -l
)
остерегайтесь ошибкисделайте, например, ls | readarray
, это не будет работать, потому что readarray будет в подоболочке из-за конвейера.Используйте только перенаправление ввода.
mapfile [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
readarray [-n count] [-O origin] [-s count] [-t] [-u fd] [-C callback] [-c quantum] [array]
Read lines from the standard input into the indexed array variable array, or from file descriptor fd if the -u option is supplied. The vari‐
able MAPFILE is the default array. Options, if supplied, have the following meanings:
-n Copy at most count lines. If count is 0, all lines are copied.
-O Begin assigning to array at index origin. The default index is 0.
-s Discard the first count lines read.
-t Remove a trailing newline from each line read.
-u Read lines from file descriptor fd instead of the standard input.
-C Evaluate callback each time quantum lines are read. The -c option specifies quantum.
-c Specify the number of lines read between each call to callback.
If -C is specified without -c, the default quantum is 5000. When callback is evaluated, it is supplied the index of the next array element
to be assigned as an additional argument. callback is evaluated after the line is read but before the array element is assigned.
If not supplied with an explicit origin, mapfile will clear array before assigning to it.
mapfile returns successfully unless an invalid option or option argument is supplied, array is invalid or unassignable, or if array is not an
indexed array.