How to join multiple lines of file names into one with custom delimiter?
This replaces the last comma with a newline:
ls -1 | tr '\n' ',' | sed 's/,$/\n/'
ls -m
includes newlines at the screen-width character (80th for example).
Mostly Bash (only ls
is external):
saveIFS=$IFS; IFS=$'\n'files=($(ls -1))IFS=,list=${files[*]}IFS=$saveIFS
Using readarray
(aka mapfile
) in Bash 4:
readarray -t files < <(ls -1)saveIFS=$IFSIFS=,list=${files[*]}IFS=$saveIFS
Thanks to gniourf_gniourf for the suggestions.