It does read by line, but you forgot to quote ${x[@]}
which meant the split+glob operator was applied to it.
readarray x < <(ls -la)
printf %s "${x[@]}"
Or to remove the last newline character from each of those lines:
readarray -t x < <(ls -la)
printf 'line: %s\n' "${x[@]}"
Or using the split+glob operator:
IFS=$'\n' # split on newline
set -o noglob
x=($(ls -la))
(contrary to readarray -t
, that method removes empty lines though (unlikely though not impossible to occur in the output of ls -la
))
If you wanted to have the list of files names (including hidden ones) without the other ls -l
information, you'd rather use:
shopt -s nullglob dotglob
x=(*)
Contrary to ls -a
, it doesn't include .
nor ..
though. If you really wanted them, you'd do instead:
shopt -s nullglob
x=(.* *)
Why do I need to write "$foo"? What happens without the quotes?
– steeldriver Aug 11 '16 at 12:48x=($(ls -la))
which results in split words and not split lines. – Peter Aug 11 '16 at 13:17