#bash #coding #programming #linux #sh
What's is the best way to do an infinite loop in BASH?
Here are some ways to do an infinite loop in BASH, using the no-op builtin (:) inside the loop…
while :;do :;done
while [[ hellnotfrozen ]];do :;done
for ((0;;));do :;done
for ((;;));do :;done
until [[ ! hellnotfrozen ]];do :;done
until [ ! hellnotfrozen ];do :;done
The first “while” loop is the most compact in terms of characters to type, and it doesn’t require the shift key like the “for” loops do – to type the “(())”.
The first “until” loop and the last "while" loop use the BASH compound command for conditional expressions and are more verbose, but they're self-commenting and much more readable and comprehensible. The "until" loop is the fastest executing (using my crude benchmark tests).
The second “until” loop uses the builtin BASH “test” command instead of the compound command, but it executes much more slowly than any of the others.