Iterate over a list of files with spaces

前端 未结 11 1616
-上瘾入骨i
-上瘾入骨i 2020-11-22 05:16

I want to iterate over a list of files. This list is the result of a find command, so I came up with:

getlist() {
  for f in $(find . -iname \"f         


        
11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 05:56

    There is also a very simple solution: rely on bash globbing

    $ mkdir test
    $ cd test
    $ touch "stupid file1"
    $ touch "stupid file2"
    $ touch "stupid   file 3"
    $ ls
    stupid   file 3  stupid file1     stupid file2
    $ for file in *; do echo "file: '${file}'"; done
    file: 'stupid   file 3'
    file: 'stupid file1'
    file: 'stupid file2'
    

    Note that I am not sure this behavior is the default one but I don't see any special setting in my shopt so I would go and say that it should be "safe" (tested on osx and ubuntu).

提交回复
热议问题