849

I am having trouble coming up with the right combination of semicolons and/or braces. I'd like to do this, but as a one-liner from the command line:

while [ 1 ]
do
    foo
    sleep 2
done
oguz ismail
  • 1
  • 16
  • 47
  • 69
Brian Deacon
  • 21,384
  • 13
  • 39
  • 41

15 Answers15

1591
while true; do foo; sleep 2; done

By the way, if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated.

$ while true
> do
>    echo "hello"
>    sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do    echo "hello";    sleep 2; done
Stefano Borini
  • 138,652
  • 96
  • 297
  • 431
  • 6
    "if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated." was not true for me. – Vincent Scheib Jan 09 '13 at 21:35
  • @VincentScheib, check `cmdhist` option if you are using bash? – Alok Singhal Mar 05 '13 at 18:56
  • Excellent! Works perfectly on Mavericks (Mac OS-X 10.9) and allows me to keep a vpn running. Openconnect disconnects after a few hours with a bad cookie error. So I put the openconnect command in a shell script, sudo su to become root, and use this cmd line: `while true; do sh /Users/myuser/bin/vpn ; done` – Blisterpeanuts Mar 04 '15 at 14:19
221

It's also possible to use sleep command in while's condition. Making one-liner looking more clean imho.

while sleep 2; do echo thinking; done
Anssi
  • 2,727
  • 1
  • 20
  • 16
  • 30
    Well this does more than just changing the syntax - it will execute the command *after* the sleep. In the previous answer, the command is executed right away. Just something to note. – DanGordon Oct 26 '16 at 16:29
  • 4
    @DanGordon ```while echo thinking ; do sleep 2 ; done``` Of course if the [while] command fails, the loop exits, so you would have to ```while echo thinking || true ; do sleep 2 ; done``` but then you are back to ```while true ; ...``` – Brian Carcich Nov 14 '19 at 21:18
91

Colon is always "true":

while :; do foo; sleep 2; done
ajaaskel
  • 927
  • 6
  • 2
  • 28
    why is colon always true? – Jürgen Paul Dec 12 '13 at 15:10
  • 14
    @Pineapple Under the Sea: From the bash man page: (in section SHELL BUILTIN COMMANDS) `: [arguments]` No effect; the command does nothing beyond expanding arguments and performing any specified redirections. A zero exit code is returned. – Adrian W Jan 17 '14 at 21:45
  • 6
    this syntax is recommended as : is part of shell itself i.e. : is a shell builtin command. – avner Feb 25 '14 at 17:59
  • 3
    @avner: recommended by whom? `true` is a builtin in `dash` and `bash` as well. And while posix requires `true` only as executable they write: "even though the shell special built-in : provides similar functionality, because *`true``* is widely used in historical scripts and *is less cryptic to novice script readers*.` ( IEEE Std 1003.1-2001). See also [this question](https://unix.stackexchange.com/questions/34677/what-is-the-difference-between-and-true) – jan-glx Jul 16 '23 at 12:56
46

You can use semicolons to separate statements:

$ while [ 1 ]; do foo; sleep 2; done
mipadi
  • 398,885
  • 90
  • 523
  • 479
36

You can also make use of until command:

until ((0)); do foo; sleep 2; done

Note that in contrast to while, until would execute the commands inside the loop as long as the test condition has an exit status which is not zero.


Using a while loop:

while read i; do foo; sleep 2; done < /dev/urandom

Using a for loop:

for ((;;)); do foo; sleep 2; done

Another way using until:

until [ ]; do foo; sleep 2; done
devnull
  • 118,548
  • 33
  • 236
  • 227
  • The `until ((0)); do foo; sleep 2; done` does not seem to work. It continuous infinite. – 030 Apr 18 '19 at 12:05
28

Using while:

while true; do echo 'while'; sleep 2s; done

Using for Loop:

for ((;;)); do echo 'forloop'; sleep 2; done

Using Recursion, (a little bit different than above, keyboard interrupt won't stop it)

list(){ echo 'recursion'; sleep 2; list; } && list;
Muhammad Soliman
  • 21,644
  • 6
  • 109
  • 75
  • 3
    Careful using units in `sleep`, like `sleep 1h`. In my zsh, it ignores the unit `h` and runs my command every 1 second. Do a quick `man sleep` to see what your environment's `sleep` command supports first. May save you a headache. – Joshua Pinter Jun 25 '20 at 16:07
17

A very simple infinite loop.. :)

while true ; do continue ; done

Fr your question it would be:

while true; do foo ; sleep 2 ; done
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mack
  • 197
  • 1
  • 2
12

For simple process watching use watch instead

user000001
  • 32,226
  • 12
  • 81
  • 108
9

I like to use the semicolons only for the WHILE statement, and the && operator to make the loop do more than one thing...

So I always do it like this

while true ; do echo Launching Spaceship into orbit && sleep 5s && /usr/bin/launch-mechanism && echo Launching in T-5 && sleep 1s && echo T-4 && sleep 1s && echo T-3 && sleep 1s && echo T-2 && sleep 1s && echo T-1 && sleep 1s && echo liftoff ; done
DanielGibbs
  • 9,910
  • 11
  • 76
  • 121
Thomas
  • 107
  • 1
  • 1
  • 1
    Well, the use of `&&` in the loop is the same as any other time really isn't it. Do you need the second thing to happen only if the first happens, then use `&&` else `;` suffices. Really, you want to keep the contents of that loop short and simple, ideally just one command, so a function or a script. – thecoshman Jul 07 '15 at 13:03
  • Careful using units in `sleep`, like `sleep 1h`. In my zsh, it ignores the unit `h` and runs my command every 1 second. Do a quick `man sleep` to see what your environment's `sleep` command supports first. May save you a headache. – Joshua Pinter Jun 25 '20 at 16:08
  • Thank you for this. I was looking for the correct way of running more than one thing in the 'do' – jjaguirre394 Feb 09 '21 at 21:21
7

If you want the while loop to stop after some condition, and your foo command returns non-zero when this condition is met then you can get the loop to break like this:

while foo; do echo 'sleeping...'; sleep 5; done;

For example, if the foo command is deleting things in batches, and it returns 1 when there is nothing left to delete.

This works well if you have a custom script that needs to run a command many times until some condition. You write the script to exit with 1 when the condition is met and exit with 0 when it should be run again.

For example, say you have a python script batch_update.py which updates 100 rows in a database and returns 0 if there are more to update and 1 if there are no more. The the following command will allow you to update rows 100 at a time with sleeping for 5 seconds between updates:

while batch_update.py; do echo 'sleeping...'; sleep 5; done;
Yep_It's_Me
  • 4,494
  • 4
  • 43
  • 66
6

You don't even need to use do and done. For infinite loops I find it more readable to use for with curly brackets. For example:

for ((;;)) { date ; sleep 1 ; }

This works in bash and zsh. Doesn't work in sh.

Victor Yarema
  • 1,183
  • 13
  • 15
2

You can try this too WARNING: this you should not do but since the question is asking for infinite loop with no end...this is how you could do it.

while [[ 0 -ne 1 ]]; do echo "it's looping";   sleep 2; done
grepit
  • 21,260
  • 6
  • 105
  • 81
2

If I can give two practical examples (with a bit of "emotion").

This writes the name of all files ended with ".jpg" in the folder "img":

for f in *; do if [ "${f#*.}" == 'jpg' ]; then echo $f; fi; done

This deletes them:

for f in *; do if [ "${f#*.}" == 'jpg' ]; then rm -r $f; fi; done

Just trying to contribute.

Roger
  • 8,286
  • 17
  • 59
  • 77
1

You can also put that loop in the background (e.g. when you need to disconnect from a remote machine)

nohup bash -c "while true; do aws s3 sync xml s3://bucket-name/xml --profile=s3-profile-name; sleep 3600; done &"
michal-michalak
  • 827
  • 10
  • 6
0

in bash 5 (or perhaps even earlier), you can also reverse the role of the : by running everything in the loop criteria but using : in the loop body instead :

while ( gdate +"%c ( %s.%-N )" && sleep 0.71  ) do :; done  # discards side-effects
      
while { gdate +"%c ( %s.%-N )" && sleep 0.71; } do :; done  # no sub-shell needed

+ gdate '+%c ( %s.%-N )'
Sun Apr 30 12:38:25 2023 ( 1682872705.498728 )
+ sleep 0.71
+ :

+ gdate '+%c ( %s.%-N )'
Sun Apr 30 12:38:26 2023 ( 1682872706.218152 )
+ sleep 0.71

... 

meanwhile zsh is even more forgiving, and willing to loop even without the :

while; do gdate +'%c ( %s.%-N )' && sleep 0.31 ; done

Sun Apr 30 12:46:09 2023 ( 1682873169.469092 )
Sun Apr 30 12:46:09 2023 ( 1682873169.789560 )
Sun Apr 30 12:46:10 2023 ( 1682873170.105635 )
Sun Apr 30 12:46:10 2023 ( 1682873170.424766 )
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11