1

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?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
krave
  • 1,629
  • 4
  • 17
  • 36

3 Answers3

4

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.

Edouard Thiel
  • 5,878
  • 25
  • 33
  • Great explanation! Do you have any advices on learning dash? I hope to learn more because I noticed that alpine has become more and more popular which has dash installed only. Is it a good idea to replace `1000` with `1d` which is more economical I guess? And is there an elegant way to stop this script? Thank you and have a great day! – krave Oct 18 '21 at 20:39
1

The : is the shell's null command. It does nothing other than evaluating its arguments and redirections. It then returns true. This is used for three purposes:

  1. When you need a command for syntactic reasons, but don't want to execute anything.
  2. When you want to perform certain parameter expansions.
  3. When you want to just truncate a file.

A use case for 1. is

while :; do
    someCommand
done

This runs someCommand repeatedly until the end of time.

Another is

while someCommand; do
    :
done

which runs someCommand until it fails.

A use case for 2. is setting default values for variables, as in

: ${EDITOR:=vi}

This assigns EDITOR=vi unless it is already set or empty.

A use case for 3. is

: >file

which truncates file. Many shells allow the non-POSIX variation >file but portable scripts should not rely on it.

Jens
  • 69,818
  • 15
  • 125
  • 179
  • Thank you for your reply! I also checked man page of dash after reading this reply. Based on the man page, I guess the general form for use case 2 is `${parameter:=word}`. It will assign default values if parameter is unset or null. Do I understand correctly that null and empty are not same? I was not able to find references about `: >file`. Do you have some references to learn dash? – krave Oct 18 '21 at 20:50
  • 1
    @krave See my answer (look for my avatar) in https://stackoverflow.com/questions/3601515 I recommend learning shell scripting instead of dash scripting. You will find yourself using several shells over your career and it pays dividends to know what is portable and what is shell-specific. – Jens Oct 19 '21 at 07:38
  • 1
    @krave I have learned a lot of my shell knowlege from high quality books, such as Peter Seebach's "Portable Shell Scripting" and Chris Johnson's "Shell Scripting Recipes", and Oliver Kiddle "From Bash to Zsh". – Jens Oct 19 '21 at 08:33
0

Here are some quotes explaining your question from https://www.gnu.org/software/bash/manual/bash.html#Shell-Syntax:

The syntax of the while command is:

while test-commands; do consequent-commands; done

Execute consequent-commands as long as test-commands has an exit status of zero. The return status is the exit status of the last command executed in consequent-commands, or zero if none was executed.


; - You can use semicolons to separate statements. Commands separated by a semicolon are executed sequentially; the shell waits for each command to terminate in turn. The return status is the exit status of the last command executed.

: - If the colon is included in a statement, the operator tests for both parameters' existence and that its value is not null; if the colon is omitted, the operator tests only for existence.

  • I noticed that you have quoted man page of bash but I asked about dash. Do you mean they are very close in syntax? Could you please give an example of 2 parameters and a colon to express the syntax mentioned from the man page? – krave Oct 18 '21 at 20:27