Search code examples
shellunixecho

How to force "echo *" to print items on separate lines in Unix?


Is * some variable? When I do echo * it lists my working directory on one line. How can I force this command to print each item on a separate line?


Solution

  • The correct way to do this is to ditch the non-portable echo completely in favor of printf:

     printf '%s\n' *
    

    However, the printf (and echo) way have a drawback: if the command is not a built-in and there are a lot of files, expanding * may overflow the maximum length of a command line (which you can query with getconf ARG_MAX). Thus, to list files, use the command that was designed for the job:

    ls -1
    

    which doesn't have this problem; or even

    find .
    

    if you need recursive lists.