0
0
Bash Scriptingscripting~5 mins

Brace expansion ({1..10}) in Bash Scripting

Choose your learning style9 modes available
Introduction
Brace expansion helps you quickly create a list of numbers or strings without typing each one. It saves time and reduces mistakes.
When you want to create multiple files named file1, file2, ..., file10 quickly.
When you need to run a command several times with numbers from 1 to 10.
When you want to loop through a sequence of numbers in a script.
When you want to generate a list of numbered folders or items.
When you want to simplify repetitive typing in the terminal.
Syntax
Bash Scripting
echo {start..end}

# Example: echo {1..10}
The numbers inside the braces define the start and end of the sequence.
You can use this in commands to generate lists or loops easily.
Examples
Prints numbers from 1 to 5 separated by spaces.
Bash Scripting
echo {1..5}
Creates a list: file1.txt file2.txt file3.txt
Bash Scripting
echo file{1..3}.txt
Creates folders named folder1, folder2, folder3, folder4.
Bash Scripting
mkdir folder{1..4}
Loops through numbers 1 to 3 and prints each.
Bash Scripting
for i in {1..3}; do echo "Number $i"; done
Sample Program
This script uses brace expansion to loop from 1 to 10 and prints each number with a label.
Bash Scripting
#!/bin/bash

# Print numbers from 1 to 10
for num in {1..10}; do
  echo "Number: $num"
done
OutputSuccess
Important Notes
Brace expansion happens before the command runs, so it generates all values first.
You can also use letters like {a..e} to expand letters instead of numbers.
If you want to include leading zeros, use {01..10} to get 01 02 ... 10.
Summary
Brace expansion quickly creates sequences of numbers or letters.
It helps automate repetitive tasks in scripts and commands.
Use it inside loops or commands to save typing and avoid errors.