Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»Bash Break and Continue

    Bash Break and Continue

    By RahulNovember 9, 20213 Mins Read

    Loops in a programming language, allows you to run commands multiple times till a particular condition. Even in Bash we have mainly three loops – until, while, and for. Sometimes you need to alter the flow of a loop and terminate it. Here comes the Break and Continue statement. They help you in terminating or managing the cycle of a loop. Here we will show you how you can use Break and Continue statements in Bash.

    Advertisement

    Bash Break Statement

    The Bash Break statement helps in terminating the current loop and passing program control to the command that follows the terminated loop. We use it to exit from a while, until, for or select loops.

    The syntax of the Break statement is: break[n]. Where [n] is an optional argument that must be greater than or equal to 1.

    Let’s understand the Break statement with an example. Here is a simple program with a for loop that will iterate over a range of values from 1-20 with an increment of 2. The conditional statement in the third line will check the expression and when val=9 is true, the break statement will run and the loop will be terminated.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    for val in {1..20..2}
    do
      If [[ $val -eq 9 ]]
      then
         break
      else
      echo "printing {val}"
    fi
    done

    Further, if you want to exit from a second loop or outer loop then you can use ‘break 2’. This will tell that loop too needs to be terminated.

    Bash Continue

    We use the Break statement to exit the entire loop when a condition is satisfied. What if you want to skip a particular block of code instead of exiting the entire loop? In such conditions, we use the Bash Continue statement. The Continue statement lets you skip the execution of a code block when the condition is satisfied or we can say that statement skips the remaining part of the code for the current iteration and passes control to the next iteration of the loop.

    The syntax of the Continue statement is: continue [n]. Again [n] is an optional argument that can be greater than or equal to 1.

    Let’s understand the Continue statement with an example. Here we have a simple program and in that when the current iterated item will be equal to 2, the continue statement will return to the beginning of the loop and continue with the next iteration.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    i=0
     
    while [[ $i -lt 5 ]]; do
      ((i++))
      if [[ "$i" == '2' ]]; then
        continue
      fi
      echo "No: $i"
    done
     
    echo 'Done!'

    The output of this program will be as follows:

    Output
    No: 1 No: 3 No: 4 No: 5 Done!
    Let’s take another example to clear the concept of the Continue statement. The following program prints the number between 1 to 31 that is divisible by 5. If the number is not divided by 5 then the Continue statement skips the ‘echo’ command and passes the control to the next iteration of the loop.
    1
    2
    3
    4
    5
    6
    for i in {1..31}; do
    if [[ $(($i%5)) -ne 0]]; then
    continue
    fi
    echo “Divisible by 5: $i”
    done
    The program will have the following output.
    Output
    Divisible by 5: 5 Divisible by 5: 10 Divisible by 5: 15 Divisible by 5: 20 Divisible by 5: 25 Divisible by 5: 30

    bash break continue shell scripts
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    10 Most Popular Linux Shells

    10 Most Popular Open Source Linux Shells

    How to Create Bash Aliases with Arguments

    How to Create Bash Aliases with Parameters

    Checking If a Command Succeeded in Bash Using the `$?` Special Variable

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Split Command in Linux With Examples (Split Large Files)
    • Test Your Internet Speed from the Linux Terminal
    • 11 Practical Example of cat Command in Linux
    • sleep Command in Linux with Examples
    • 20 Basic Linux Commands for the Beginners (Recommended)
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.