0 votes
in JavaScript by
What are the different types of loops in Bash and can you provide a brief example of each?

1 Answer

0 votes
by

Bash supports three types of loops: for, while, and until.

The ‘for’ loop iterates over a list of items. For example:

for i in {1..5}; do echo $i; done

This prints numbers 1 to 5.

The ‘while’ loop executes as long as the condition is true. Example:

i=1; while [ $i -le 5 ]; do echo $i; let "i++"; done

This also prints numbers 1 to 5.

The ‘until’ loop runs until the condition becomes true. Here’s an example:

i=1; until [ $i -gt 5 ]; do echo $i; let "i++"; done

Again, this prints numbers 1 to 5.

...