Bash の単一行 while ループの構文 質問する

Bash の単一行 while ループの構文 質問する

セミコロンと中括弧の正しい組み合わせが思いつきません。これをやりたいのですが、コマンド ラインから 1 行で実行します。

while [ 1 ]
do
    foo
    sleep 2
done

ベストアンサー1

while true; do foo; sleep 2; done

ちなみに、コマンド プロンプトで (表示されているように) 複数行として入力し、上矢印で履歴を呼び出すと、正しく句読点が付けられた 1 行で表示されます。

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

おすすめ記事