Print one file per line using echoWhy is printf better than echo?Bash - echo line by line, ignoring the space between the linescreate XML file using bash script`ip addr` in one-line per interfaceInteractively add arguments line-by-line in bashNested echo command in backticksHow to print out “-E” in bash echo?Write a program that read from a file and print the line with line numberbash echo the command line executed at the command line itself (not in a script)Restricting Bash Filename ExpansionRead lines into array, one element per line using bash
What is "industrial ethernet"?
Find the common ancestor between two nodes of a tree
Am I legally required to provide a (GPL licensed) source code even after a project is abandoned?
Why is oilcloth made with linseed oil?
Find All Possible Unique Combinations of Letters in a Word
Is "Busen" just the area between the breasts?
Non-misogynistic way to say “asshole”?
What was the flower of Empress Taytu?
Greeting with "Ho"
Should the party get XP for a monster they never attacked?
Is the specular reflection on a polished gold sphere white or gold in colour?
Is declining an undergraduate award which causes me discomfort appropriate?
Is the continuity test limit resistance of a multimeter standard?
Dmesg full of I/O errors, smart ok, four disks affected
Counterfeit checks were created for my account. How does this type of fraud work?
Why isn't my calculation that we should be able to see the sun well beyond the observable universe valid?
A word for delight at someone else's failure?
Justifying Affordable Bespoke Spaceships
Prisoner on alien planet escapes by making up a story about ghost companions and wins the war
Why don't we have a weaning party like Avraham did?
How did Gollum enter Moria?
Is there any proof that high saturation and contrast makes a picture more appealing in social media?
What does this Swiss black on yellow rectangular traffic sign with a symbol looking like a dart mean?
Helping ease my back pain by studying 13 hours everyday , even weekends
Print one file per line using echo
Why is printf better than echo?Bash - echo line by line, ignoring the space between the linescreate XML file using bash script`ip addr` in one-line per interfaceInteractively add arguments line-by-line in bashNested echo command in backticksHow to print out “-E” in bash echo?Write a program that read from a file and print the line with line numberbash echo the command line executed at the command line itself (not in a script)Restricting Bash Filename ExpansionRead lines into array, one element per line using bash
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
How can I print a list of files/directories one-per-line using echo
?
I can replace spaces with newlines, but this doesn't work if the filenames contain spaces:
$ echo small*jpg
small1.jpg small2.jpg small photo 1.jpg small photo 2.jpg
$ echo small*jpg | tr ' ' 'n'
small1.jpg
small2.jpg
small
photo
1.jpg
small
photo
2.jpg
I know I can do this with ls -d1
, but is it also possible using echo
?
bash echo bash-expansion
add a comment |
How can I print a list of files/directories one-per-line using echo
?
I can replace spaces with newlines, but this doesn't work if the filenames contain spaces:
$ echo small*jpg
small1.jpg small2.jpg small photo 1.jpg small photo 2.jpg
$ echo small*jpg | tr ' ' 'n'
small1.jpg
small2.jpg
small
photo
1.jpg
small
photo
2.jpg
I know I can do this with ls -d1
, but is it also possible using echo
?
bash echo bash-expansion
add a comment |
How can I print a list of files/directories one-per-line using echo
?
I can replace spaces with newlines, but this doesn't work if the filenames contain spaces:
$ echo small*jpg
small1.jpg small2.jpg small photo 1.jpg small photo 2.jpg
$ echo small*jpg | tr ' ' 'n'
small1.jpg
small2.jpg
small
photo
1.jpg
small
photo
2.jpg
I know I can do this with ls -d1
, but is it also possible using echo
?
bash echo bash-expansion
How can I print a list of files/directories one-per-line using echo
?
I can replace spaces with newlines, but this doesn't work if the filenames contain spaces:
$ echo small*jpg
small1.jpg small2.jpg small photo 1.jpg small photo 2.jpg
$ echo small*jpg | tr ' ' 'n'
small1.jpg
small2.jpg
small
photo
1.jpg
small
photo
2.jpg
I know I can do this with ls -d1
, but is it also possible using echo
?
bash echo bash-expansion
bash echo bash-expansion
asked 15 hours ago
EmmaVEmmaV
1,27411337
1,27411337
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
echo
can't be used to output arbitrary data anyway, use printf
instead which is the POSIX replacement for the broken echo
utility to output text.
printf '%sn' small*jpg
You can also do:
printf '%s' small*jpg
to output the list in NUL delimited records (so it can be post-processed; for instance using GNU xargs -r0
; remember that the newline character is as valid as space or any character in a filename).
Before POSIX came up with printf
, ksh already had a print
utility to replace echo
. zsh copied it and added a -l
option to print the arguments one per line:
print -rl -- small*jpg
ksh93 added a -f
option to print
for printf
like printing. Copied by zsh
as well, but not other ksh implementations:
print -f '%sn' -- small*jpg
Note that all of those still print an empty line if not given any argument. A better println
can be written as a function as:
println() printf '%sn' "$@"
add a comment |
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).
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.
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
2 hours ago
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f525399%2fprint-one-file-per-line-using-echo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
echo
can't be used to output arbitrary data anyway, use printf
instead which is the POSIX replacement for the broken echo
utility to output text.
printf '%sn' small*jpg
You can also do:
printf '%s' small*jpg
to output the list in NUL delimited records (so it can be post-processed; for instance using GNU xargs -r0
; remember that the newline character is as valid as space or any character in a filename).
Before POSIX came up with printf
, ksh already had a print
utility to replace echo
. zsh copied it and added a -l
option to print the arguments one per line:
print -rl -- small*jpg
ksh93 added a -f
option to print
for printf
like printing. Copied by zsh
as well, but not other ksh implementations:
print -f '%sn' -- small*jpg
Note that all of those still print an empty line if not given any argument. A better println
can be written as a function as:
println() printf '%sn' "$@"
add a comment |
echo
can't be used to output arbitrary data anyway, use printf
instead which is the POSIX replacement for the broken echo
utility to output text.
printf '%sn' small*jpg
You can also do:
printf '%s' small*jpg
to output the list in NUL delimited records (so it can be post-processed; for instance using GNU xargs -r0
; remember that the newline character is as valid as space or any character in a filename).
Before POSIX came up with printf
, ksh already had a print
utility to replace echo
. zsh copied it and added a -l
option to print the arguments one per line:
print -rl -- small*jpg
ksh93 added a -f
option to print
for printf
like printing. Copied by zsh
as well, but not other ksh implementations:
print -f '%sn' -- small*jpg
Note that all of those still print an empty line if not given any argument. A better println
can be written as a function as:
println() printf '%sn' "$@"
add a comment |
echo
can't be used to output arbitrary data anyway, use printf
instead which is the POSIX replacement for the broken echo
utility to output text.
printf '%sn' small*jpg
You can also do:
printf '%s' small*jpg
to output the list in NUL delimited records (so it can be post-processed; for instance using GNU xargs -r0
; remember that the newline character is as valid as space or any character in a filename).
Before POSIX came up with printf
, ksh already had a print
utility to replace echo
. zsh copied it and added a -l
option to print the arguments one per line:
print -rl -- small*jpg
ksh93 added a -f
option to print
for printf
like printing. Copied by zsh
as well, but not other ksh implementations:
print -f '%sn' -- small*jpg
Note that all of those still print an empty line if not given any argument. A better println
can be written as a function as:
println() printf '%sn' "$@"
echo
can't be used to output arbitrary data anyway, use printf
instead which is the POSIX replacement for the broken echo
utility to output text.
printf '%sn' small*jpg
You can also do:
printf '%s' small*jpg
to output the list in NUL delimited records (so it can be post-processed; for instance using GNU xargs -r0
; remember that the newline character is as valid as space or any character in a filename).
Before POSIX came up with printf
, ksh already had a print
utility to replace echo
. zsh copied it and added a -l
option to print the arguments one per line:
print -rl -- small*jpg
ksh93 added a -f
option to print
for printf
like printing. Copied by zsh
as well, but not other ksh implementations:
print -f '%sn' -- small*jpg
Note that all of those still print an empty line if not given any argument. A better println
can be written as a function as:
println() printf '%sn' "$@"
edited 15 hours ago
answered 15 hours ago


Stéphane ChazelasStéphane Chazelas
322k57615985
322k57615985
add a comment |
add a comment |
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).
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.
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
2 hours ago
add a comment |
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).
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.
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
2 hours ago
add a comment |
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).
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.
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).
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.
edited 9 hours ago
answered 10 hours ago


fra-sanfra-san
2,2801824
2,2801824
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
2 hours ago
add a comment |
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
2 hours ago
I strongly recommend setting
IFS
back to normal after anything like this.– Gordon Davisson
2 hours ago
I strongly recommend setting
IFS
back to normal after anything like this.– Gordon Davisson
2 hours ago
add a comment |
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f525399%2fprint-one-file-per-line-using-echo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown