Syntax for a single-line Bash infinite while loop

前端 未结 13 1384
长情又很酷
长情又很酷 2020-11-29 14:18

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 [         


        
13条回答
  •  無奈伤痛
    2020-11-29 14:46

    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
    

提交回复
热议问题