Bash 中单行 while 循环的语法 [英] Syntax for a single-line while loop in Bash

查看:30
本文介绍了Bash 中单行 while 循环的语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在想出分号和/或大括号的正确组合时遇到了麻烦.我想这样做,但作为命令行中的单行:

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

顺便说一句,如果您在命令提示符下将其输入为多行(如您所示),然后使用向上箭头调用历史记录,您将在一行中找到它,标点正确.

By the way, if you type it as a multiline (as you are showing) at the command prompt and then call the history with arrow up, you will get it on a single line, correctly punctuated.

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

这篇关于Bash 中单行 while 循环的语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆