For example, I want to rename a file abc to bd.

Why do the two brace expansions seem not to work?

Consider the following example.

$ touch abc
$ mv {a,}b{c,d}
mv: target `bd' is not a directory

How shall I make brace expansion work?


Two brace expansions do work, they just don't work the way you want them to:

$ touch abc
$ mv {a,}b{c,d}
mv: target `bd' is not a directory
$ echo mv {a,}b{c,d}
mv abc abd bc bd

They are expanded separately - effectively the first one is expanded, leaving you with mv ab{c,d} b{c,d} and then the second is expanded, leaving you with mv abc abd bc bd.

  • how can I use brace expansions to do what I want? – Tim Feb 24 '15 at 21:59
  • @Tim, a brace expansion will produce $number_of_commas+1 words, and using multiple brace expansions within a single argument will create the cross product. Since you want to only have 2 arguments at the end, you can only use one comma, which means that the only way to do it with brace expansion would be mv {abc,bd} - but that's harder to type and less obvious than just mv abc bd. – godlygeek Feb 24 '15 at 22:38
  • in case a, b and c are replaced with long strings, that is why I try to use brace expansion. Or shall I use brace expansion one each time and multiple times? – Tim Feb 24 '15 at 22:54
  • Without having more details it's hard to say, but the obvious solution would just be to use variables. a=foo b=bar c=baz d=bang; mv $a$b$c $b$d – godlygeek Feb 24 '15 at 22:59
  • 1
    @Tim, given that you can't really do what you want to do with brace expansion, I'd ask a new question that explains exactly what you're trying to do, with a more detailed example, and ask for solutions to that problem. – godlygeek Feb 24 '15 at 23:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.