← 返回命令列表

Linux command

until 命令

文本

涉及管道、覆盖或删除,执行前请先确认路径和参数。

常用示例

Loop until condition

until [condition]; do [commands]; done

Wait for file

until [ -f [file] ]; do sleep 1; done

Wait for process

until pgrep [process]; do sleep 1; done

Counter loop

until [ $i -gt 10 ]; do echo $i; i=$((i+1)); done

说明

until is a shell loop construct that repeatedly executes a block of commands as long as a condition evaluates to false (non-zero exit status). It is the logical opposite of while, which loops while a condition is true. The condition is tested before each iteration, so if it is already true when the loop starts, the body never executes. The most common use of until is polling for a condition to become true, such as waiting for a network host to come online, a file to appear, or a process to start. Combined with sleep, it provides a simple way to implement retry logic in shell scripts. The exit status of an until loop is the exit status of the last command executed in the body. If the body never executes because the condition is already true, the exit status is zero.

FAQ

What is the until command used for?

until is a shell loop construct that repeatedly executes a block of commands as long as a condition evaluates to false (non-zero exit status). It is the logical opposite of while, which loops while a condition is true. The condition is tested before each iteration, so if it is already true when the loop starts, the body never executes. The most common use of until is polling for a condition to become true, such as waiting for a network host to come online, a file to appear, or a process to start. Combined with sleep, it provides a simple way to implement retry logic in shell scripts. The exit status of an until loop is the exit status of the last command executed in the body. If the body never executes because the condition is already true, the exit status is zero.

How do I run a basic until example?

Run `until [condition]; do [commands]; done` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

Where can I find more until examples?

This page includes 4 examples for until, plus related commands for nearby Linux tasks.