echo
only uses spaces to separate the strings it receives as arguments.
Since your question is tagged bash, here is what help echo
in bash
says (emphasis mine):
Display the ARGs, separated by a single space character and followed by a newline, on the standard output.
Similar statements are found in the documentation for other implementations. E.g. that of echo
from coreutils
, which you would likely find on GNU/Linux:
echo
writes each given string to standard output, with a space between each and a newline after the last one.
If you really want echo
to print your file names on separate lines you have to feed them as a single string:
$ touch "small1.jpg" "small2.jpg" "small photo 1.jpg" "small photo 2.jpg"
$ (set -- small*.jpg; IFS='
'; echo "$*")
small1.jpg
small2.jpg
small photo 1.jpg
small photo 2.jpg
Here we are leveraging the behavior of the *
special parameter: within double-quotes it expands to a single word in which the elements of the array of positional parameters are concatenated using the first character of the IFS
variable (which we set to a newline).
The (...)
syntax is used to execute the commands in a subshell environment—we generally don't want to affect the main one.
Note, however, that all echo
's limitations still apply, as mentioned in Stéphane's answer, and therefore its use in this scenario is not advisable.
IFS
back to normal after anything like this. – Gordon Davisson Jun 18 '19 at 01:18