I see this line of script from this tutorial on VS Code doc.
while sleep 1000; do :; done
I know the functionality of this line is to prevent process exiting. But I don't understand the syntax. Could you please help to explain the script? Do you have advices on learning dash syntax?
The :
is a shell builtin, that stands for true
. The true
command does nothing and succeeds (by returning 0).
The while
command follows the syntax:
while command1 ; do command2 ; done
The command1
is executed, and when finished, the shell checks if the command has succeed or failed. In the first case, the command2
is executed, then the command1
is fired again, else the loop is stopped.
In your example, sleep 1000
waits for 1000 seconds, succeeds, then true
is called, then the loop is called again and again (sleep
will never failed, this is an infinite loop).
This kind of one-liner might in fact be of interest to keep a script alive, while running other things like co-routines or signal traps; it is very economical since during the sleep
, the process is stopped: here every 1000 seconds, the process is resumed and stopped right again.