Home Syntax for a single-line Bash infinite while loop
Reply: 11

Syntax for a single-line Bash infinite while loop

Brian Deacon
1#
Brian Deacon Published in 2009-08-17 16:31:47Z

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
Stefano Borini
2#
Stefano Borini Reply to 2009-08-17 16:32:32Z
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
mipadi
3#
mipadi Reply to 2009-08-17 16:32:41Z

You can use semicolons to separate statements:

$ while [ 1 ]; do foo; sleep 2; done
DanielGibbs
4#
DanielGibbs Reply to 2011-08-30 00:29:09Z

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
Roger
5#
Roger Reply to 2011-07-02 16:32:48Z

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.

Anssi
6#
Anssi Reply to 2012-11-06 10:34:41Z

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
ajaaskel
7#
ajaaskel Reply to 2012-11-06 13:35:49Z

Colon is always "true":

while :; do foo; sleep 2; done
devnull
8#
devnull Reply to 2013-10-16 07:44:32Z

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
Peter Mortensen
9#
Peter Mortensen Reply to 2016-11-26 20:10:46Z

A very simple infinite loop.. :)

while true ; do continue ; done

Fr your question it would be:

while true; do foo ; sleep 2 ; done
user000001
10#
user000001 Reply to 2016-12-14 11:36:20Z

For simple process watching use watch instead

CPU 100
11#
CPU 100 Reply to 2017-02-07 21:50:12Z

You can try this too

while [[ 0 -ne 1 ]]; do echo "it's looping";   sleep 2; done
Yep_It's_Me
12#
Yep_It's_Me Reply to 2018-01-18 02:30:48Z

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;
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.341231 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO