Bash and filenames with spaces

前端 未结 6 1802
情深已故
情深已故 2020-12-01 03:23

The following is a simple Bash command line:

grep -li \'regex\' \"filename with spaces\" \"filename\"

No problems. Also the following works

6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-01 03:49

    With Bash 4, you can also use the builtin mapfile function to set an array containing each line and iterate on this array:

    $ tree
    .
    ├── a
    │   ├── a 1
    │   └── a 2
    ├── b
    │   ├── b 1
    │   └── b 2
    └── c
        ├── c 1
        └── c 2
    
    3 directories, 6 files
    $ mapfile -t files < <(find -type f)
    $ for file in "${files[@]}"; do
    > echo "file: $file"
    > done
    file: ./a/a 2
    file: ./a/a 1
    file: ./b/b 2
    file: ./b/b 1
    file: ./c/c 2
    file: ./c/c 1
    

提交回复
热议问题