← All commands
Topic hub Terminal & Shell Scripting
Windows Linux Terminal & Shell Scripting Terminal

Windows while (goto loop) equivalent in Linux

Repeatedly executes commands as long as a condition remains true.

Windows while (goto loop)
Linux equivalents
Debian/Ubuntu same command
while [ condition ]; do ... done
Fedora same command
while [ condition ]; do ... done
Arch same command
while [ condition ]; do ... done

Overview

What this command does

Repeatedly executes commands as long as a condition remains true.

Migration tip: Use while true; do ... done for an infinite loop (exit with Ctrl+C or break). while read line is the standard pattern for processing a file line by line.

Practical tasks

Common use cases

Loop while a condition is true

Repeatedly executes commands as long as a condition remains true.

while true; do

Infinite loop

while read line; do

Read file line by line

break

Exit the loop

Ready to run

Copyable Linux commands

Review paths, device names, package names and permissions before running any command.

while read line; do
echo "Line: $line"
done < input.txt
while ! ping -c 1 google.com; do
echo "Waiting for network..."
sleep 5
done

Walkthrough

Examples with explanations

# Read file line by line:
while read line; do
  echo "Line: $line"
done < input.txt

# Retry until success:
while ! ping -c 1 google.com; do
  echo "Waiting for network..."
  sleep 5
done

Reference

Common options and variations

Command or optionUse
while true; doInfinite loop
while read line; doRead file line by line
breakExit the loop
continueSkip to next iteration

Distro differences

Debian/Ubuntu vs Fedora vs Arch

This command is the same on Debian/Ubuntu, Fedora and Arch Linux when the relevant tool is installed.

DistributionPackage manager / baseEquivalent commandDifference to notice
Debian/Ubuntu same command while [ condition ]; do ... done Same command on this distribution.
Fedora same command while [ condition ]; do ... done Same command on this distribution.
Arch same command while [ condition ]; do ... done Same command on this distribution.

Keep learning

Related Terminal & Shell Scripting commands