Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Linux Bash have a do-while loop? [duplicate]

After some search on the Internet, it appears that Bash doesn't have a do-while loop.

Is this correct? Is there a reliable source to confirm this (the lack of evidence that there is a do-while loop is not an argument that there isn't one, perhaps a statement is only unpopular)?

Is it possible to define directives oneself and thus implement a do-while loop? There is an algorithmic way to transform a do-while-loop in a while-loop, but this is not the scope of this question.

like image 546
Willem Van Onsem Avatar asked Jan 04 '15 01:01

Willem Van Onsem


People also ask

Do a while loop in bash?

The while loop is used to performs a given set of commands an unknown number of times as long as the given condition evaluates to true. The while statement starts with the while keyword, followed by the conditional expression. The condition is evaluated before executing the commands.

What is the Do while command in Linux?

while command in Linux is used to repeatedly execute a set of command as long as the COMMAND returns true. The test command is given and all other commands are executed till the given command's result satisfies, when the command's result become false, the control will be out from the while command.

What does && do in bash?

"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0).

Can you do for loops in bash?

A bash for loop is a bash programming language statement which allows code to be repeatedly executed. A for loop is classified as an iteration statement i.e. it is the repetition of a process within a bash script. For example, you can run UNIX command or task 5 times or read and process list of files using a for loop.


2 Answers

bash (or Posix shells in general) don't have an explicit syntax for a post-test loop (commonly known as a "do-while" loop) because the syntax would be redundant. The while compound statement allows you to write pre-test, post-test or mid-test loops, all with the same syntax.

Here's the semantics of a shell while loop, from Posix:

The format of the while loop is as follows:

while compound-list-1
do
  compound-list-2
done

The compound-list-1 shall be executed, and if it has a non-zero exit status, the while command shall complete. Otherwise, the compound-list-2 shall be executed, and the process shall repeat.

A "compound list" is a sequence of commands; the exit status of a compound list is the exit status of the last command in the list.

That means that you could think of a while loop as being written as follows:

while
  optional-pre-test-compound-list
  condition
do
  post-test-compound-list
done

That is, there is no requirement that the condition to be tested immediately follows the while keyword. So the equivalent to the C syntax:

do statements while (test);

is

while statements; test do :; done

The : between the do and the done is required, because the shell grammar does not allow empty statements. Since : is not a metacharacter, it must have whitespace or a metacharacter before and after it; otherwise, it would be parsed as part of the preceding or succeeding token. Since it's parsed as a command, it also needs a semicolon or newline after it; otherwise the done is treated as an argument to :.

like image 102
rici Avatar answered Oct 21 '22 05:10

rici


No, bash has no do-while loop. The manual section 3.2.4.1 Looping Constructs lists until, while and for. Note there is no do-while.

like image 27
Elliott Frisch Avatar answered Oct 21 '22 04:10

Elliott Frisch