Assuming you have a string with spaces as separators:
newline_separated=${space_separated// /$'\n'}
However you're probably asking the wrong question. (Not necessarily, for example this might come up in a makefile.) A space-separated list of file names doesn't really work: what if one of the file names contained spaces?
If a program receives file names as arguments, don't join them with spaces. Use "$@"
to access them one by one. Although echo "$@"
prints the arguments with spaces in between, that's due to echo
: it prints its arguments with spaces as separators. somecommand "$@"
passes the file names as separate arguments to the command. If you want to print the arguments on separate lines, you can use
printf '%s\n' "$@"
If you do have space-separated file names and you want to put them in an array to work on them:
split_list () {
local IFS=' ' flags='+f'
if [[ $- = *f* ]]; then flags=; fi
set -f
eval "$1=($2)"
set $flags
}
split_list array '/path/to/file1 /path/to/file2 /path/to/file3'
for x in "${array[@]}"; do …