Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is an empty loop invalid in shell script?

Tags:

bash

shell

loops

I wanted to make my shell script wait infinitely and thought the below code would do it.

#!/bin/bash
while true
do
done

However, the above script reports syntax error.

./Infinite_Loop.sh: line 4: syntax error near unexpected token `done'

./Infinite_Loop.sh: line 4: `done'

Unlike programming languages, why does shell script expect at least one statement in loops?

like image 811
Beginner Avatar asked Dec 02 '22 11:12

Beginner


1 Answers

Another option is just to set a NOP(no op) which is basically, do nothing.

In bash, the equivalent for a NOP is :.

while true; do
  :
done
like image 110
criw Avatar answered Dec 06 '22 07:12

criw