Unix Shell Programming: Add a blank line when printing

后端 未结 5 1430
盖世英雄少女心
盖世英雄少女心 2021-01-28 13:17

I am trying to list all the files in the directory but how would you separate each of the files by a blank line? basically each file displayed by separated by a blank line? I am

5条回答
  •  囚心锁ツ
    2021-01-28 14:21

    Here's one:

    find -printf '%p\n\n'
    

    A slightly worse (but more portable) one:

    ls | sed 's|$|\n|'
    

    A more convoluted one:

    ls | while read f; do
        echo "$f"
        echo
    done
    

    And here is what you should not ever do:

    for f in `ls`; do
        echo "$f"
        echo
    done
    

    EDIT:

    And, as mentioned by Nija, the simple shell-only one:

    for f in *; do
        echo "$f"
        echo
    done
    

    This one has the disadvantage that on many shells * by default expands to itself, rather than an empty string when no files exist.

提交回复
热议问题