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;








4















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?










share|improve this question




























    4















    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?










    share|improve this question
























      4












      4








      4








      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?










      share|improve this question














      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 17 at 11:53









      EmmaVEmmaV

      1,2791 gold badge13 silver badges37 bronze badges




      1,2791 gold badge13 silver badges37 bronze badges




















          2 Answers
          2






          active

          oldest

          votes


















          13














          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() 





          share|improve this answer
































            3














            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.






            share|improve this answer

























            • 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













            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
            );



            );













            draft saved

            draft discarded


















            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









            13














            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() 





            share|improve this answer





























              13














              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() 





              share|improve this answer



























                13












                13








                13







                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() 





                share|improve this answer















                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() 






                share|improve this answer














                share|improve this answer



                share|improve this answer








                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























                    3














                    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.






                    share|improve this answer

























                    • 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















                    3














                    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.






                    share|improve this answer

























                    • 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













                    3












                    3








                    3







                    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.






                    share|improve this answer















                    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.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    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 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

















                    • 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
















                    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

















                    draft saved

                    draft discarded
















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Get product attribute by attribute group code in magento 2get product attribute by product attribute group in magento 2Magento 2 Log Bundle Product Data in List Page?How to get all product attribute of a attribute group of Default attribute set?Magento 2.1 Create a filter in the product grid by new attributeMagento 2 : Get Product Attribute values By GroupMagento 2 How to get all existing values for one attributeMagento 2 get custom attribute of a single product inside a pluginMagento 2.3 How to get all the Multi Source Inventory (MSI) locations collection in custom module?Magento2: how to develop rest API to get new productsGet product attribute by attribute group code ( [attribute_group_code] ) in magento 2

                    Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

                    Magento 2.3: How do i solve this, Not registered handle, on custom form?How can i rewrite TierPrice Block in Magento2magento 2 captcha not rendering if I override layout xmlmain.CRITICAL: Plugin class doesn't existMagento 2 : Problem while adding custom button order view page?Magento 2.2.5: Overriding Admin Controller sales/orderMagento 2.2.5: Add, Update and Delete existing products Custom OptionsMagento 2.3 : File Upload issue in UI Component FormMagento2 Not registered handleHow to configured Form Builder Js in my custom magento 2.3.0 module?Magento 2.3. How to create image upload field in an admin form