Lesson 4 — Simple For Loops
Learning Objectives
Section titled “Learning Objectives”By the end of this lesson, you will be able to:
- Explain what a for loop does and why it is useful
- Write a basic for loop in Bash
- Loop over a fixed list of values
- Loop over a set of files using a wildcard pattern (
*.fastq) - Combine loops with variables and
basename
1. Why Loops?
Section titled “1. Why Loops?”In Lesson 3, you wrote a script that processed one file. What if you have ten files? You could copy and paste the same commands ten times — but that defeats the purpose of scripting.
A for loop solves this. It lets you write a set of commands once and have Bash repeat them for each item in a list.
2. The For Loop Structure
Section titled “2. The For Loop Structure”Here is the structure of a Bash for loop:
for VARIABLE in item1 item2 item3do command using $VARIABLEdoneBreaking it down:
forstarts the loopVARIABLEis a name you choose — Bash will assign it each item from the list, one at a timeinis followed by the list of itemsdomarks the start of the commands to repeatdonemarks the end of the loop
Everything between do and done runs once for every item in the list.
3. A Simple Example
Section titled “3. A Simple Example”Let us start with a small example before using real files:
#!/bin/bash
for SAMPLE in SRR001 SRR002 SRR003do echo "Processing sample: $SAMPLE"doneOutput:
Processing sample: SRR001Processing sample: SRR002Processing sample: SRR003What happened:
- Bash assigned
SRR001toSAMPLE, then ranecho - Bash assigned
SRR002toSAMPLE, then ranecho - Bash assigned
SRR003toSAMPLE, then ranecho - The list was exhausted — the loop ended
The commands between do and done can be anything: echo, fastqc, cp, mv — as many commands as you need.
4. Looping Over Files
Section titled “4. Looping Over Files”Typing out every sample name by hand is still repetitive. Instead, you can use a wildcard pattern to match files automatically.
The * character matches any sequence of characters. So:
*.fastqmeans “every file whose name ends in .fastq.”
Here is a loop that prints the name of every FASTQ file in the paired reads folder:
#!/bin/bash
for FILE in Training/short_reads/paired/*.fastqdo echo $FILEdoneOutput:
Training/short_reads/paired/SRR1553607_1.fastqTraining/short_reads/paired/SRR1553607_2.fastqBash expanded *.fastq into the list of matching files and looped over each one automatically. If you add more .fastq files to that folder, the loop will include them the next time you run the script — without any changes to the script itself.
5. Combining Loops with basename
Section titled “5. Combining Loops with basename”Now combine what you learned in Lesson 3 with a loop. Inside the loop, use basename to get the clean sample name from each file path:
#!/bin/bash
for FILE in Training/short_reads/paired/*.fastqdo SAMPLE=$(basename $FILE .fastq) echo "Sample: $SAMPLE" echo "File: $FILE"doneOutput:
Sample: SRR1553607_1File: Training/short_reads/paired/SRR1553607_1.fastqSample: SRR1553607_2File: Training/short_reads/paired/SRR1553607_2.fastqOn each pass through the loop:
$FILEholds the full path to one FASTQ file$SAMPLEholds the clean name extracted from that path- Both are available to use in any command inside the loop
6. Running FastQC in a Loop
Section titled “6. Running FastQC in a Loop”Here is a loop that does real work — running FastQC on every FASTQ file in the folder:
#!/bin/bash
for FILE in Training/short_reads/paired/*.fastqdo SAMPLE=$(basename $FILE .fastq) echo "Running FastQC on: $SAMPLE" fastqc $FILEdoneSave this as loop_fastqc.sh, make it executable, and run it:
chmod +x loop_fastqc.sh./loop_fastqc.shFastQC will run on each .fastq file in turn. This script already does everything the manual approach did — but in a form that scales to any number of files.
Summary
Section titled “Summary”- A for loop repeats commands for each item in a list
- The loop variable (
FILE,SAMPLE, etc.) takes a new value on each pass through the loop *.fastqmatches all files ending in.fastqat the specified pathbasenamecan be used inside a loop to extract a clean sample name from each file path- Adding new files to the folder automatically includes them in future runs — no script changes needed
In the final lesson, you will combine everything — shebang, variables, basename, and a loop — into a complete, well-structured script ready for real use.