How To

Bash Range And Sequence Expression Explained With Examples

Introduction

Bash range and sequence expressions are useful when you want to generate a list of numbers or characters automatically. Instead of writing values one by one, you can use a short Bash syntax to create a sequence. This is very helpful in Bash scripting, especially when working with loops, file names, backups, automation tasks, and cybersecurity scripts.

If you are learning Bash scripting, understanding range expressions will help you write shorter and cleaner scripts. You can use them to count numbers, repeat tasks, create multiple files, scan IP addresses, or process numbered logs.

What Is A Bash Range?

A Bash range is a sequence of numbers or letters written inside curly braces {}.

Basic syntax:

{start..end}

Example:

echo {1..5}

Output:

1 2 3 4 5

This command prints numbers from 1 to 5.

Bash Number Range Example

Create a Bash script:

nano range-example.sh

Add the following code:

#!/bin/bashfor number in {1..5}do    echo "Number: $number"done

Save and run the script:

chmod +x range-example.sh./range-example.sh

Output:

Number: 1Number: 2Number: 3Number: 4Number: 5

This is useful when you want to repeat a command for a fixed number of times.

Bash Alphabet Range Example

Bash can also create letter ranges.

#!/bin/bashfor letter in {a..e}do    echo "Letter: $letter"done

Output:

Letter: aLetter: bLetter: cLetter: dLetter: e

You can also use uppercase letters:

echo {A..Z}

Bash Range With Step Value

You can use a step value to skip numbers.

Syntax:

{start..end..step}

Example:

echo {1..10..2}

Output:

1 3 5 7 9

This prints numbers from 1 to 10 with a step of 2.

Another example:

#!/bin/bashfor number in {2..10..2}do    echo "Even number: $number"done

Create Multiple Files Using Bash Range

Range expressions are useful for creating multiple files quickly.

touch file{1..5}.txt

This creates:

file1.txt file2.txt file3.txt file4.txt file5.txt

You can check them using:

ls file*.txt

Cybersecurity Example: Ping Multiple IP Addresses

You can use Bash range expressions to test multiple IP addresses.

#!/bin/bashfor ip in 192.168.1.{1..5}do    echo "Checking $ip"    ping -c 1 "$ip"done

This script pings IP addresses from 192.168.1.1 to 192.168.1.5.

Conclusion

Bash range and sequence expressions are simple but powerful. They help you generate numbers, letters, file names, and IP ranges quickly. They are commonly used with for loops in Bash scripts.

For beginners, learning Bash range syntax is important because it makes scripts shorter, cleaner, and easier to manage. You can use it for Linux automation, file creation, backups, network checks, and cybersecurity scripting.

Cyber Defence

Recent Posts

Best OSINT Tools for Journalists 2026: Verify Sources, Images and Claims

Journalists use OSINT to verify public information before publishing. In 2026, misinformation, AI-generated images, fake…

7 hours ago

Install Docker on Ubuntu 20.04: Complete Step-by-Step Guide

Docker is an open-source platform that lets you package and run applications inside containers. Each container…

18 hours ago

Install PostgreSQL on Ubuntu: Database Setup and Admin Guide

PostgreSQL (often called Postgres) is an open-source relational database system. It supports advanced features like JSON…

19 hours ago

Install Xrdp Remote Desktop on Ubuntu: Setup and Connect

Xrdp is an open-source server that lets you connect to your Ubuntu machine from another computer…

19 hours ago

Tomcat 9 on Ubuntu 20.04: Install, Configure, and Start

Apache Tomcat is an open-source web server and Java servlet container. It is one of the…

19 hours ago

Automatic Updates on Ubuntu: Set Up unattended-upgrades

Keeping your Ubuntu system updated is one of the best ways to protect it. Security…

20 hours ago