I want my directory listing output in an array. readarray x < <( ls -la )printf %s ${x[@]}So I wrote this, which works but it splits by space and not by newline. I can't find a newline parameter at readarray manual, so I'm kind of curious if there is a neat way to solve that request.Please do not suggest to use find (I am restricted to use ls -la).I use bash 4.3.
Write ls to array (split by newline)
bash;ls;array
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 newlineset -o noglobx=($(ls -la))(contrary to readarray -rt, 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 dotglobx=(*)Contrary to ls -a, it doesn't include . nor .. though. If you really wanted them, you'd do instead:shopt -s nullglobx=(.* *)