Loops
Part of the Bash course
So far every script you have written runs each line exactly once, top to bottom. That is fine for short scripts, but real work is repetitive: rename fifty files, check ten servers, print a greeting for everyone on a list. Typing the same command fifty times is not a plan. A loop is a block of commands that Bash repeats, either once for every item in a list or for as long as a condition holds true.
Picture a stack of index cards on your desk, one name per card. Reading them out loud one at a time, in order, until the stack is empty, is exactly what a for loop does with a list of words. You already know how to test one value with if. A for loop is what you reach for when you want to run the same commands against many values instead of just one. ...
Want to learn Bash Loops in a real environment?
Click below, get a fresh Linux box with Bash ready to go, and work through Loops hands-on with an AI tutor that knows your code.
Start this lessonLoopsWhat you'll learn
- For loop
- While loop
- Until loop
- For f in *.txt globbing
- C-style for loop
- Break and continue
Read this lesson in your language
All lessons in this course
12 lessons- 1What Is Bash20 min
- 2Shebang and Your First Script25 min
- 3Variables and Quoting25 min
- 4Conditionals and Tests30 min
- 5Loops25 min
- 6Functions25 min
- 7Arrays25 min
- 8String and Arithmetic Expansion25 min
- 9IO Redirection and Pipes25 min
- 10Error Handling and Traps30 min
- 11Debugging and Best Practices25 min
- 12Capstone: Server Bootstrap Script60 min
