Given a command that outputs multiple strings separated by null bytes, what's the best (fastest) way to convert this into an array in bash.
eg:
git ls-files -z
For bash 4.4 and later:
mapfile -d '' arrayVar < <(git ls-files -z)
# note the space here^
For bash 4.3 and earlier:
arrayVar=( )
while IFS= read -r -d '' item; do
arrayVar+=( "$item" )
done < <(git ls-files -z)
#note ^here is a space
Note that you cannot use pipes regardless of bash version, ex:
# !! Example of incorrect code:
git ls-files -z | mapfile -d '' arrayVar
will not work (unless lastpipe is enabled and active, see shopt), because pipes implicitly create a subshell. See BashFAQ#024.
IFS=? Won't IFS be ignored if there is only one variable to read into?
IFS=really necessary?help readarray(or ratherhelp mapfile) suggests thatIFSis not used sincemapfiledoesn't use wordsplitting likeread, but always splits on newlines or the symbols given by-d.IFS= readarray -t -d '' MY_ARRAY < <(git ls-files -z)Could include this as an answer instead.IFSisn't needed. Thanks