Instructor: Let's say I want to create a backup of a file. I want the backup to retain the same name as the file but just append .backup on the end. I'll just go ahead and create a dummy file here. If I wanted to create a backup of the file, I would do copy index.js.
Instead of passing the file name there again, I can do braces -- that's why it's called a brace expansion as it uses braces -- and do comma and then .backup. Let's see how that works. Cool. That works just like we wanted.
Another thing I can do with brace expansions is create several folders with the same structure. If I wanted to do a monorepo style setup, I could do packages and then I'll have my different package names here.
Then I want each one to have a source folder in it. Let's do that and then see how that came out. Yeah, that's the folder structure I wanted.
Let's dive into a little bit how it works a bit more, so let's do a basic example. Let's do echo and do pre. I'm going to do A, B, C there and braces and then post. Brace expansions follow this kind of format. There's what's called a preamble which is the string that occurs before our braces, and then there's a postscript which occurs after it.
This is a list of strings. We can see when we run it, it basically iterates through this list and then prepends or appends the preamble and the postscript as needed.
Know that this also supports empty items in the list. If we go back to our copy example, we'll just use echo instead. We had index.js, and then we had an empty element and m.backup.
If we echo that out, we can see that that's exactly the arguments that we need to pass to the copy command. Basically, when we run this with copy, the brace expansion gets interpreted, and these arguments gets passed to copy and then copied exactly as we need to, which just saves a lot of typing along the way.
Another thing brace expansions can do is rearranges. For example, if I do echo, brace, two dots, and then 10, it's going to interpret this as an integer sequence, and it fills in the missing numbers from that range.
The same applies to letters. This can be useful for generating a lot of test files. For example, if I do touch and I do tests through 10, let's list that out. What you see here is all my test files that I created.
Note that this works because the touch command accepts multiple inputs. Not all commands do that, so you have to check the commands documentation before you can be sure something like this will work.