bash : brace expansion for automatic substitution

Bash expands a list of strings separated by commas with in braces to a list of strings separated by spaces. For example

[unixite@theunixtips:~/> echo 111-{aa,bb,cc}+{xx,yy,zz}-222
111-aa+xx-222 111-aa+yy-222 111-aa+zz-222 111-bb+xx-222 111-bb+yy-222 111-bb+zz-222 111-cc+xx-222 111-cc+yy-222 111-cc+zz-222
unixite@theunixtips:~/>

There should not be any space between suffix/prefix and the start/end bracket. Otherwise it is considered as a separate list. Now coming to a real world example where you can make use of this.

mv process.log{,.old} #This will move process.log to process.log.old
mv process.log.{old,oldest} #This will move process.log.old to process.log.oldest
  • Pingback: Howto Rollover a file when Size Exceeds Using Unix find | The Unix Tips()

  • William L. Dye (“willdye”)

    Note that if you have to use quotes to handle filenames with special characters, you can’t use this form:

    mv “filename with spaces.txt{,.old}” ;# <— doesn't work

    Instead you have to use this form:

    mv "filename with spaces.txt"{,".extension with spaces"}

    If the extension might have special characters, such as when the string is in a variable, you should probably quote that as well:

    prefix="file name with spaces.txt"
    suffix=".extension with spaces"
    mv "$prefix"{,"$suffix"}