← Back to DevBytes

Advanced Fish: Loops Techniques

Advanced Fish Shell Loops: Techniques for Efficient Scripting

The Fish shell (Friendly Interactive SHell) is known for its user‑friendly syntax, autosuggestions, and powerful scripting capabilities. Among its most useful features are loops—constructs that let you repeat commands or blocks of code. While basic for and while loops are straightforward, mastering advanced loop techniques can drastically improve your scripts’ efficiency, readability, and maintainability. This tutorial covers what advanced loop techniques in Fish are, why they matter, how to use them with practical examples, and the best practices to follow.

What Are Loops in Fish Shell?

Loops in Fish allow you to execute a set of commands multiple times. The shell provides two primary loop types:

Advanced loop techniques go beyond simple iteration. They include using loop control statements (break and continue), nested loops, iterating over complex data structures (like arrays of arrays), parallel execution, and combining loops with Fish’s built‑in commands and functions.

Why Advanced Loop Techniques Matter

As your scripts grow, simple loops may become inefficient or hard to read. Advanced techniques help you:

Mastering these techniques turns you from a casual Fish scripter into an efficient shell programmer.

How to Use Advanced Loops in Fish

1. The Basic for Loop – Foundation

A standard for loop iterates over a list. The syntax is:

for variable in list_of_values
    commands
end

Example – printing numbers 1 through 5:

for i in 1 2 3 4 5
    echo "Number: $i"
end

2. Using break and continue

break exits the loop entirely; continue skips the rest of the current iteration and moves to the next.

Break example – stop at the first even number:

for n in 1 3 4 5 6
    if test (math "$n % 2") -eq 0
        echo "Found even: $n"
        break
    end
    echo "Odd: $n"
end

Output:

Odd: 1
Odd: 3
Found even: 4

Continue example – skip odd numbers:

for n in 1 2 3 4 5
    if test (math "$n % 2") -eq 1
        continue
    end
    echo "Even: $n"
end

Output:

Even: 2
Even: 4

3. Iterating Over Arrays and Lists

Fish arrays are space‑separated lists. Use the for loop directly:

set fruits apple banana cherry
for fruit in $fruits
    echo "I like $fruit"
end

To iterate over a list from a command output, use command substitution:

for file in (ls *.txt)
    echo "Processing: $file"
end

Note: This splits on newlines by default; use string split0 for null‑delimited output (e.g., find . -print0).

4. The while Loop – Conditional Repetition

while repeats until its condition is false. Example – count down from 5:

set count 5
while test $count -gt 0
    echo "Count: $count"
    set count (math "$count - 1")
end

Output:

Count: 5
Count: 4
Count: 3
Count: 2
Count: 1

5. Nested Loops – Handling Multi‑Dimensional Data

You can place one loop inside another. Use different variable names to avoid conflicts.

Example – print a multiplication table for 1–3:

for i in 1 2 3
    for j in 1 2 3
        set product (math "$i * $j")
        echo "$i × $j = $product"
    end
    echo "---"
end

Output:

1 × 1 = 1
1 × 2 = 2
1 × 3 = 3
---
2 × 1 = 2
2 × 2 = 4
2 × 3 = 6
---
3 × 1 = 3
3 × 2 = 6
3 × 3 = 9
---

6. Parallel Execution with Background Jobs

Fish can run loop iterations in parallel by appending & inside the loop body and then waiting for all jobs to finish.

for url in $urls
    curl -O "$url" &
end
wait
echo "All downloads complete."

Caution: Too many parallel jobs can overwhelm your system. Use a limit (e.g., set max_jobs 4 and track job count).

7. Looping Over Ranges Using seq or math

Fish does not have a built‑

🚀 Need a reliable AI agent for your project?

Deploy Hermes Agent in 10 minutes. Managed hosting, zero DevOps.

Get Started — $23.99/mo
← Back to all articles