break

break

The break command in Linux is used to exit a loop from within the loop. This is useful if you want to exit a loop early, for example if you find the condition that you are testing for is met.

The break command is used in the following syntax:

break [n]

The n is optional and specifies the number of loops to skip before exiting. If n is not specified, the break command will exit the current loop.

For example, the following code will print the numbers from 1 to 10, but will exit the loop after printing the number 5:

for i in {1..10}
do
  echo $i
  if [ $i -eq 5 ]; then
    break
  fi
done

This code will print the following output:

1
2
3
4
5

The break command is a simple and easy-to-use command that can be used to exit a loop from within the loop. It is a versatile command that can be used in a variety of contexts.

Here are some additional things to note about the break command:

  • The break command can be used to exit any type of loop, including for loops, while loops, and do-while loops.

  • The break command can be used to exit the current loop, or to skip a specified number of loops.

  • The break command is a simple and easy-to-use command.

help

Last updated