Bash brace expansion after a path slash
The brace expansion syntax accepts commas, but it does not accept a space after the comma. In many programming languages, spaces after commas are commonplace, but not here. In Bash, the presence of an unquoted space prevents brace expansion from being performed.
Remove the space, and it will work:
cp ~/some/dir/{my-file-to-rename.bin,new-name-of-file.bin}
While not at all required, note that you can move the trailing .bin
outside the braces:
cp ~/some/dir/{my-file-to-rename,new-name-of-file}.bin
If you want to test the effect of brace expansion, you can use echo
or printf '%s '
, or printf
with whatever format string you prefer, to do that. (Personally I just use echo
for this, when I am in Bash, because Bash's echo
builtin doesn't expand escape sequences by default, and is thus reasonably well suited to checking what command will actually run.) For example:
ek@Io:~$ echo cp ~/some/dir/{my-file-to-rename,new-name-of-file}.bin
cp /home/ek/some/dir/my-file-to-rename.bin /home/ek/some/dir/new-name-of-file.bin
string{foo, bar}
isn't brace expansion; it's the two words string{foo,
and bar}
. To use brace expansion, the braces need to be within the same word. You'll have to either remove the extra space if you don't need it, or quote it if you do need it:
$ printf "%s\n" aa{bb, cc}
aa{bb,
cc}
$ printf "%s\n" aa{bb,cc}
aabb
aacc
$ printf "%s\n" aa{bb," cc"}
aabb
aa cc