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 fortrue. Thetruecommand does nothing and succeeds (by returning 0).The
whilecommand follows the syntax:The
command1is executed, and when finished, the shell checks if the command has succeed or failed. In the first case, thecommand2is executed, then thecommand1is fired again, else the loop is stopped.In your example,
sleep 1000waits for 1000 seconds, succeeds, thentrueis called, then the loop is called again and again (sleepwill 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.