🐧 Linux / Shell Scripting
Intermediate
How do you write loops in bash?
Answer
Bash supports three loop types. for loop: for i in 1 2 3; do echo $i; done or C-style for ((i=0; i<10; i++)); do ... done. while loop: while [ condition ]; do ... done — commonly used to read files line by line: while IFS= read -r line; do echo "$line"; done < file.txt. until loop: runs until condition becomes true — opposite of while. Loop controls: break exits the loop, continue skips to the next iteration. Iterate over files: for f in *.txt; do process "$f"; done. Always quote loop variables to handle filenames with spaces.
Previous
How do you write conditional statements in bash?
Next
What is the difference between [ ] and [[ ]] in bash?