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
while [ condition ]; do ... done
while [ condition ]; do ... done
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 option | Use |
|---|---|
while true; do | Infinite loop |
while read line; do | Read file line by line |
break | Exit the loop |
continue | Skip 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.
| Distribution | Package manager / base | Equivalent command | Difference 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