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
If a high rpm motor is run at lower rpm, will it produce more torque?
where clause to retrieve case record which are older than 12 months and 1 day in soql
What shortcut does ⌦ symbol in Camunda macOS app indicate and how to invoke it?
How hard is it to sell a home which is currently mortgaged?
For people who believe in Jesus and not the devil, what happend in the desert?
“Transitive verb” + interrupter+ “object”?
How to determine what is the correct level of detail when modelling?
can’t run a function against EXEC
How exactly is a normal force exerted, at the molecular level?
Symbol for "not absolutely continuous" in Latex
Was "I have the farts, again" broadcast from the Moon to the whole world?
Zombie diet, why humans?
Are there any vegetarian astronauts?
Dual statement category theory
How to convert object fill in to fine lines?
Is there a short way to check uniqueness of values without using 'if' and multiple 'and's?
Why does this function call behave sensibly after calling it through a typecasted function pointer?
Golf the smallest circle!
Should I include salary information on my CV?
Does ultrasonic bath cleaning damage laboratory volumetric glassware calibration?
In native German words, is Q always followed by U, as in English?
Alphabet completion rate
Why Is Abelian Gauge Theory So Special?
Generate and graph the Recamán Sequence
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 Jun 17 at 11:53
EmmaVEmmaV
1,2791 gold badge13 silver badges37 bronze badges
1,2791 gold badge13 silver badges37 bronze badges
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()
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).
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.
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
Jun 18 at 1:18
@GordonDavisson Right, thank you. Answer amended.
– fra-san
Jun 18 at 6:46
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()
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()
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()
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()
edited Jun 17 at 12:05
answered Jun 17 at 12:00
Stéphane ChazelasStéphane Chazelas
323k57 gold badges623 silver badges990 bronze badges
323k57 gold badges623 silver badges990 bronze badges
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).
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.
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
Jun 18 at 1:18
@GordonDavisson Right, thank you. Answer amended.
– fra-san
Jun 18 at 6:46
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).
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.
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
Jun 18 at 1:18
@GordonDavisson Right, thank you. Answer amended.
– fra-san
Jun 18 at 6:46
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).
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.
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.
edited Jun 18 at 6:44
answered Jun 17 at 17:18
fra-sanfra-san
2,3001 gold badge8 silver badges24 bronze badges
2,3001 gold badge8 silver badges24 bronze badges
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
Jun 18 at 1:18
@GordonDavisson Right, thank you. Answer amended.
– fra-san
Jun 18 at 6:46
add a comment |
I strongly recommend settingIFS
back to normal after anything like this.
– Gordon Davisson
Jun 18 at 1:18
@GordonDavisson Right, thank you. Answer amended.
– fra-san
Jun 18 at 6:46
I strongly recommend setting
IFS
back to normal after anything like this.– Gordon Davisson
Jun 18 at 1:18
I strongly recommend setting
IFS
back to normal after anything like this.– Gordon Davisson
Jun 18 at 1:18
@GordonDavisson Right, thank you. Answer amended.
– fra-san
Jun 18 at 6:46
@GordonDavisson Right, thank you. Answer amended.
– fra-san
Jun 18 at 6:46
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