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

while true; do foo; sleep 2; done

顺便说一句,如果您在命令提示符下键入多行(如您所示),然后用箭头向上调用历史记录,则您将在单行中得到它,并且正确地加上标点符号。

$ while true
> do
>    echo "hello"
>    sleep 2
> done
hello
hello
hello
^C
$ <arrow up> while true; do    echo "hello";    sleep 2; done

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

冒号总是“真实的”:

while :; do foo; sleep 2; done
链接地址: http://www.djcxy.com/p/17462.html

上一篇: 使用bash中的单个命令将默认值分配给shell变量

下一篇: 线Bash无限while循环