This guide shows you how to use for loops in Bash to repeat commands over a list of items, numbers, files, or command output. You’ll learn the syntax, common patterns, and practical examples you can run in your terminal right away. No prior scripting experience is needed beyond basic command-line navigation.
What You’ll Need
- A Linux, macOS, or Windows system with Bash installed (Windows users can use Git Bash, WSL, or Cygwin).
- Access to a terminal or command prompt.
- Basic familiarity with running commands like
echo,ls, andcd.
Step-by-Step
- Understand the basic syntax. A Bash
forloop looks like this:for variable in list; do commands doneThe
variabletakes each item fromlistone by one, and thecommandsrun each time. The semicolons anddo/donekeywords are required. - Loop over a simple list of words. Type this in your terminal:
for fruit in apple banana cherry; do echo "I like $fruit" donePress Enter. You’ll see three lines, each with a different fruit. The
$fruitexpands to the current item. - Loop over a range of numbers. Use curly braces
{start..end}:for i in {1..5}; do echo "Number $i" doneThis prints 1 through 5. You can also use a step value:
{1..10..2}gives 1, 3, 5, 7, 9. - Use the C-style syntax for more complex conditions. If you need a counter with an increment, use:
for ((i=0; i<5; i++)); do echo "Iteration $i" doneThis is similar to C/Java loops. The condition
i<5runs the loop untilireaches 5. - Loop over files in a directory. Use a wildcard to match files:
for file in *.txt; do echo "Found $file" doneThis prints all
.txtfiles in the current directory. If no files match, the loop still runs once with the literal*.txt— see Common Mistakes. - Loop over command output. Use
$(command)to generate the list:for user in $(cut -d: -f1 /etc/passwd); do echo "User: $user" doneThis prints every username from the passwd file. Be careful with spaces in output — see Tips.
- Use the
breakandcontinuecontrols. Inside a loop,breakstops the loop entirely, andcontinueskips to the next iteration:for i in {1..10}; do if [ $i -eq 5 ]; then break fi echo $i doneThis prints 1 through 4 and then stops.
- Run a loop as a one-liner. You can write a loop on a single line by separating parts with semicolons:
for i in {1..3}; do echo $i; doneThis is handy for quick terminal commands.
- Put loops in a script. Create a file named
loop.shwithnano loop.sh, add the loop, then runbash loop.shor make it executable withchmod +x loop.shand run./loop.sh.
Common Mistakes
- Missing semicolons or
do/done. The syntax is strict. If you seesyntax error near unexpected token, check that you have semicolons beforedoand after the list. - Using
for i in {1..5}without the braces. If you writefor i in 1..5, it treats1..5as a literal string, not a range. - Spaces around the equals sign in C-style loops.
for ((i = 0; i < 5; i++))is okay, butfor ((i=0; i<5; i++))is more common. Do not put spaces between the variable and=in the assignment part. - Forgetting to quote variables. If a filename contains spaces,
for file in $listbreaks. Always quote:for file in "$list"— but note that quoting the whole list makes it one item. Use arrays orfindfor filenames with spaces. - Looping over an empty glob. If no files match
*.txt, Bash passes the literal*.txtas the item. Check withshopt -s nullglobto avoid this. - Using
echo $iwithout quotes. While it often works, quoting"$i"prevents word splitting and globbing issues.
Tips
- Use arrays for lists with spaces. Instead of
for fruit in apple "red banana", define an array:fruits=("apple" "red banana"), then loopfor fruit in "${fruits[@]}". - Use
seqfor dynamic ranges.for i in $(seq 1 2 10)gives 1,3,5,7,9. This is useful when the step or end is a variable. - Test with
echofirst. Before running a destructive loop (like deleting files), replace the actual command withechoto see what would happen. - Use
set -xto debug. Addset -xat the top of a script to print each command as it runs, thenset +xto turn it off. - Prefer
findfor complex file operations. If you need to process files recursively or with conditions,findwith-execis often more robust than aforloop. - Use
continueto skip specific items. For example, skip hidden files:for file in *; do [[ $file == .* ]] && continue; echo $file; done.
Troubleshooting
- Syntax error near unexpected token 'done'. Usually a missing
door a missing semicolon before it. Check that you havedoafter the list and a semicolon beforedoif on the same line. - Loop runs only once. If you use a variable as the list, like
for i in $list, and$listis a single string, it loops once. Use an array oreval(not recommended) to iterate over multiple items. - Loop doesn't run at all. If the list is empty, the loop body never executes. For ranges, ensure the start is less than or equal to the end. For globs, check that files exist.
- Files with spaces are split. Use
findwith-print0andwhile IFS= read -r -d '', or setIFSto newline:IFS=$'\n'before the loop. - Command substitution output has newlines. By default,
$(command)splits on newlines, but if the output has spaces, it splits on spaces too. Usewhile IFS= read -r line; do ... done < <(command)to preserve lines. - Permission denied when running a script. You need to make the script executable:
chmod +x script.sh, or run it withbash script.sh.
Frequently Asked Questions
Can I use a variable as the range end?
Yes, but not directly with brace expansion. Use for ((i=1; i<=$end; i++)) or for i in $(seq 1 $end).
How do I loop through lines of a file?
Use while IFS= read -r line; do echo "$line"; done < file.txt. This handles spaces and special characters correctly.
What's the difference between for and while loops?
for loops iterate over a fixed list or range, while while loops run as long as a condition is true. Use while when you don't know how many iterations you need.
Can I nest for loops?
Yes, you can put one loop inside another. Just ensure each has its own do/done pair and use different variable names.
How do I loop over arguments passed to a script?
Use for arg in "$@"; do echo "$arg"; done. The "$@" expands to all arguments, each properly quoted.
Is there a way to loop forever?
Use for ((;;)); do echo "Ctrl+C to stop"; done or while true; do ...; done. Both run indefinitely until you interrupt them.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Campus Tech Innovation Hubs: What They Are
- Poco C51 Airtel Prepaid Locked Variant India?
- Jenkins vs GitHub Actions: Which CI/CD Fits Your Team?
- Turn Off Backup Encryption in iTunes?
- Shared VPS Hosting: Is It Better?
- Disk Defrag Pro: What It Does and Whether You Need It