使用带大括号扩展名的 touch 创建多个空格分隔的文件

Ter*_*ium 4 command-line bash

如果我这样做:

touch {1,2,3}.txt
Run Code Online (Sandbox Code Playgroud)

我明白了

1.txt 2.txt 3.txt
Run Code Online (Sandbox Code Playgroud)

但如果我这样做

touch {"File 1", "File 2"}.txt
Run Code Online (Sandbox Code Playgroud)

我没有得到预期'File 1.txt' 'File 2.txt'。在这种情况下,正确的方法是什么?

ste*_*ver 5

后面的空格会,导致 shell 将表达式解析为两个单独的标记,而不是作为大括号扩展:

$ printf '%s\n' {"File 1", "File 2"}.txt
{File 1,
File 2}.txt
Run Code Online (Sandbox Code Playgroud)

您只需要删除空格:

$ printf '%s\n' {"File 1","File 2"}.txt
File 1.txt
File 2.txt
Run Code Online (Sandbox Code Playgroud)

所以

$ touch {"File 1","File 2"}.txt
$ ls
'File 1.txt'  'File 2.txt'
Run Code Online (Sandbox Code Playgroud)

更紧凑的是,您还可以使用touch "File "{1,2}.txttouch File\ {1,2}.txt