In ZSH, what's the idiomatic way to run a program repeatedly until it fails?
- User
In ZSH, what's the idiomatic way to run a program repeatedly until it fails?
Assistant
Here are a few common ways to run a program repeatedly in zsh until it fails:
# Repeat command until failure, storing exit code each time while ! cmd; do :; done # Repeat command until failure, stopping on first non-zero exit while cmd; do :; done # Repeat command forever, exiting loop on failure while true; do cmd || break; done # Repeat command forever, catching failures with trap trap 'exit' ERR while true; do cmd; doneThe simplest approach is to use a
whileloop with the!negation operator to check if the command failed. This will run the command over and over until it returns a non-zero exit code.The
trapmethod catches any failure and exits the loop immediately. This avoids repeating the failed command.In all cases, the
:;or;is used as a no-op when you don't need anything else in the body of the loop.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.