Saturday 9 June 2018

Syntax for a single-line Bash infinite while loop



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


Answer



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
$ while true; do echo "hello"; sleep 2; done

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...