> ## Content Index
> Fetch the complete content index at: https://mfitzp.ghost.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Use bash brace expansion to save {hours,days}
- URL: https://mfitzp.ghost.io/use-bash-brace-expansion-to-save-hours-days/
- Published: 2011-12-05T00:00:00.000Z
- Updated: 2023-06-26T09:39:41.000Z
- Author: Martin Fitzpatrick
- Tags: Tutorials, Bash, #Import 2024-03-14 08:20, #Import 2026-08-24 14:26

Brace expansion is one of the most powerful bash tricks with the potential to save you considerable time. Bash brace expansion takes a list of arguments and expands them into separate arguments to the command.

```Bash
$ echo hello{1,2,3}
hello1 hello2 hello3

```

🧠

**Braces can also be nested for more complex combinations.*

Brace expansion is extremely useful for the creation of backup files.

```Bash
$ cp ~/.bash_profile{,.bak}

```

You will now have your bash\_profile file backed up in `~/.bash_profile.bak`

💡

**Note the `,` at the beginning of the brace expansion - indicating an empty field. In the first instance this brace expansion will expand to the command with nothing after it (i.e. the original file), then secondly adding the `.bak` to create the backup.*

You can do this in reverse to copy the file back

```Bash
$ cp ~/.bash_profile{.bak,}

```

Or check for file changes from backup with diff

```Bash
$ echo Hello! >> ~/.bash_profile.bak
$ diff ~/.bash_profile{.bak,}
23d22
< Hello!

```

Brace expansion also supports number ranges which can be useful for when you need to create a lot of sequentially ordered objects. For example

```Bash
$ mkdir tmp{1..100}

```

Will produce a number of folders named tmp1, tmp2, tmp3, tmp4, etc.. in the current directory.

Be careful with the use of spaces as these will stop expansion working. If you have any text containing spaces, wrap it in quotes. However, the expansion won't work inside quotes either, for example the following will not work – 

```Bash
echo Use bash brace expansion to save {hours,days} echo "Use bash brace expansion to save {hours,days}"
```

While the following will work – 

```Bash
echo "Use bash brace expansion to save "{hours,days} echo "Use bash brace expansion to save"{" hours"," days"}
```

In the second example, note that the space has been moved inside the expansion set, so each of them must be wrapped in quotes.