有时您正在处理包含空格的文件和文件夹。问题是任何时候您尝试将包含空格的文件/文件夹通过管道传输到另一个命令行程序时,包含空格的文件/文件夹都会被解释为单独的参数而不是单个参数。例如,考虑以下目录树:
Folder With Spaces
Folder With Spaces/FolderWithoutSpaces
Folder With Spaces/FolderWithoutSpaces/file with spaces.txt
FolderWithoutSpaces
FolderWithoutSpaces/fileWithoutSpaces.txt
Run Code Online (Sandbox Code Playgroud)
如果您尝试运行诸如“grep 'some text' $(find . -type f)”之类的 shell 命令,您将获得以下输出:
grep: ./Folder: No such file or directory
grep: With: No such file or directory
grep: Spaces/FolderWithoutSpaces/file: No such file or directory
grep: with: No such file or directory
grep: spaces.txt: No such file or directory
Run Code Online (Sandbox Code Playgroud)
最大的问题是,您如何将包含空格的文件/文件夹作为命令行程序的参数进行管道传输?