From the course: Learning Linux Shell Scripting
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Using break and continue - Linux Tutorial
From the course: Learning Linux Shell Scripting
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…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.