Fundamentals 7 min read

Master Bash: How to Write and Run Powerful For Loops in Shell Scripts

This guide explains the syntax, creation, and execution of Bash for loops, covering basic structures, numeric and string iteration, step values, conditional breaks, and essential commands like chmod and ./script.sh to automate repetitive tasks efficiently.

Liangxu Linux
Liangxu Linux
Liangxu Linux
Master Bash: How to Write and Run Powerful For Loops in Shell Scripts

When you need to repeat the same commands in a shell script, using a for loop saves time and reduces errors. Loops are a fundamental programming construct available in Bash, allowing you to automate repetitive tasks such as updating a series of numbers or strings.

Basic for‑loop structure

The generic syntax is:

for item in [LIST]
do
  [COMMANDS]
 done

Or, iterating over a numeric range:

for VARIABLE in 1 2 3 4 5 .. N
 do
  command1
  command2
  commandN
 done

Here VARIABLE holds the current value, the list after in defines the iteration count, and the commands between do and done are executed each cycle.

Creating and running a Bash script

Open a terminal, launch a text editor (the examples use nano), and create a file with a .sh extension: linuxmi@linuxmi:~/www$ nano LinuxMi.com.sh Write your script, then save and exit (Ctrl+X). Make the script executable: linuxmi@linuxmi:~/www$ chmod +x LinuxMi.com.sh Run it with:

linuxmi@linuxmi:~/www$ ./LinuxMi.com.sh

Printing integers

Simple example that prints three numbers:

#!/usr/bin/bash
for i in 1 2 3
 do
  echo "Current # $i"
 done

Output shows each iteration value. The script must be saved, permission set, and executed as shown above.

Alternative numeric loops

Using brace expansion to define start and end values:

for i in {1..3}
 do
  echo "Current value # $i: example 2"
 done

This runs three times, printing the values sequentially.

Loops with step values

Specify a step (increment) with a third number in the brace expression:

for i in {1..10..2}
 do
  echo "Number = $i"
 done

The loop runs with a gap of 2, producing output 1, 3, 5, 7, 9.

Iterating over strings

Bash for loops can also handle strings:

for name in LinuxMi linuxmi.com www.linuxmi.com
 do
  echo "My name is $name"
 done

The loop iterates over the three supplied strings and prints each.

Conditional break inside a loop

You can stop a loop early with break based on a condition:

for distro in LinuxMi Debian CentOS Ubuntu; do
  if [[ "$distro" == 'CentOS' ]]; then
    break
  fi
  echo "distro: $distro"
 done

echo 'All done!'

The loop terminates when distro equals CentOS, printing the first two entries and then the final message.

Why use loops in Bash?

Loops are essential for reducing repetitive code, adhering to the DRY principle, and making scripts more maintainable. They are comparable to loops in other languages such as Python, and mastering them greatly enhances the power of your Linux shell scripts.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Automationfor loop
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.