From the course: Learning Linux Shell Scripting

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using break and continue

Using break and continue - Linux Tutorial

From the course: Learning Linux Shell Scripting

Start my 1-month free trial

Using break and continue

- [Instructor] Both loops import two special instructions, break and continue. Break causes the current loop to terminate and it begins executing the instructions after the done statement. Let's try it out. We'll add an if statement in the loop before printing the current name. So, here we'll put our if and we'll say $NAME equals Tracy and make sure we got a space before the square bracket. Then, and we'll put a break. And then we'll put the fi. We'll save this off with a control S. Go back to the terminal. Once again, we'll type in Stacy, Tracy and Lacy and you notice what we get. Now we get the hello Stacy and as soon as it detects the Tracy, the break statement causes it to stop execution of the loop and the loop gets terminated and we exit our program. The continue statement goes immediately to the top of the loop. Let's replace the break with continue and see what happens. So, here we'll change break to continue. We'll save this off with a control S. Go to the terminal. Once…

Contents