In Bash, you can use the continue
command to skip the remaining commands in a loop and move on to the next iteration.
Here is an example of how you can use the continue
command in a for
loop:
# Define an array of numbers numbers=(1 2 3 4 5) # Iterate over the array for number in "${numbers[@]}"; do # Skip the iteration if the number is 3 if [ "$number" -eq 3 ]; then continue fi # Print the number echo "$number" doneSoww:ecruw.lautturi.com
This will print the numbers 1
, 2
, 4
, and 5
, skipping the number 3
because of the continue
command. The output will be:
1 2 4 5
Here is an example of how you can use the continue
command in a while
loop:
# Initialize the counter variable counter=1 # Run the loop while the counter is less than or equal to 5 while [ "$counter" -le 5 ]; do # Skip the iteration if the counter is 3 if [ "$counter" -eq 3 ]; then counter=$((counter+1)) continue fi # Print the counter echo "$counter" # Increment the counter counter=$((counter+1)) done
This will print the numbers 1
, 2
, 4
, and 5
, skipping the number 3
because of the continue
command. The output will be:
1 2 4 5
Overall, the continue
command is a useful tool for skipping the remaining commands in a loop and moving on to the next iteration. It allows you to easily control the flow of a loop and skip over certain iterations as needed.