In a Bash loop, what does 'continue' do?

Study for the OSCP Linux Exam. Use our flashcards and multiple-choice questions to test your skills. Each query comes with detailed hints and explanations to enhance your preparedness. Get ready to conquer the exam!

Multiple Choice

In a Bash loop, what does 'continue' do?

Explanation:
In Bash loops, the continue command stops executing the rest of the loop body for the current iteration and immediately starts the next iteration. It’s a way to skip any remaining statements in the loop when a certain condition is met without exiting the loop entirely. Remember how it differs from other controls: break would exit the loop completely, and exit could terminate the whole script. continue only affects the loop flow, not the entire script. If you’re inside nested loops, you can use an optional numeric argument like continue 2 to skip to the next iteration of the outer loop, not just the innermost one. By default, continue means continue 1 (the next iteration of the current loop). Example: for i in 1 2 3 4 5; do if [ "$i" -eq 3 ]; then continue fi echo "$i" done This prints 1, 2, 4, 5 because when i is 3, the rest of the loop body is skipped and the loop moves on to the next value.

In Bash loops, the continue command stops executing the rest of the loop body for the current iteration and immediately starts the next iteration. It’s a way to skip any remaining statements in the loop when a certain condition is met without exiting the loop entirely.

Remember how it differs from other controls: break would exit the loop completely, and exit could terminate the whole script. continue only affects the loop flow, not the entire script.

If you’re inside nested loops, you can use an optional numeric argument like continue 2 to skip to the next iteration of the outer loop, not just the innermost one. By default, continue means continue 1 (the next iteration of the current loop).

Example:

for i in 1 2 3 4 5; do

if [ "$i" -eq 3 ]; then

continue

fi

echo "$i"

done

This prints 1, 2, 4, 5 because when i is 3, the rest of the loop body is skipped and the loop moves on to the next value.

Subscribe

Get the latest from Passetra

You can unsubscribe at any time. Read our privacy policy