How to pipe a null separated string into an array in bash?

Viewed 294

Given a command that outputs multiple strings separated by bytes, whats the best (fastest) way to convert this into an array in bash.

eg: git ls-files -z

1 Answers

For bash 4.4 and later only:

readarray -d '' array < <(git ls-files -z)

For backwards compatibility with bash 3.x and 4.0 through 4.3:

array=( )
while IFS= read -r -d '' item; do
  array+=( "$item" )
done < <(git ls-files -z)
Related