Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»Bash Break and Continue

    Bash Break and Continue

    RahulBy RahulNovember 8, 20213 Mins ReadUpdated:November 9, 2021

    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.

    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.

    Shell
    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.

    Shell
    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.
    Shell
    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
    Previous ArticleHow To Add a Git Remote Repository
    Next Article How To Install Python 3.10 on Ubuntu, Debian & Linux Mint

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Bash Sequence Expression (Define Range)

    Updated:January 12, 20222 Mins Read

    Bash Select Command (Create Menu in Shell Scripts)

    Updated:December 10, 20212 Mins Read

    Bash String Concatenate

    Updated:January 5, 20222 Mins Read

    Shell Script to Add Two Numbers

    Updated:July 7, 20212 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • How to Install Sublime Text 4 on Ubuntu 22.04
    • How to Enable / disable Firewall in Windows
    • How to Install JAVA on Ubuntu 22.04
    • Switching Display Manager in Ubuntu – GDM, LightDM & SDDM
    • Changing the Login Screen Background in Ubuntu 22.04 & 20.04
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

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