4

If I do this:

touch {1,2,3}.txt

I get

1.txt 2.txt 3.txt

But if I do this

touch {"File 1", "File 2"}.txt

I don't get expected 'File 1.txt' 'File 2.txt'. What is the proper approach in this case?

2 Answers 2

5

Whitespace after the , causes the shell to parse your expression as two separate tokens, instead of as a brace expansion:

$ printf '%s\n' {"File 1", "File 2"}.txt
{File 1,
File 2}.txt

You just need to remove the whitespace:

$ printf '%s\n' {"File 1","File 2"}.txt
File 1.txt
File 2.txt

So

$ touch {"File 1","File 2"}.txt
$ ls
'File 1.txt'  'File 2.txt'

More compactly, you could also use touch "File "{1,2}.txt or touch File\ {1,2}.txt

0

Alternatively, also in bash, this can be done in a for loop like so:

for f in "fil 1" "file 2"; do
    touch "$f".txt
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .