温馨提示×

linux的exit能否跳出循环

小樊
85
2024-12-27 20:18:35
栏目: 智能运维
Linux服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

是的,exit 命令可以用来跳出循环

#!/bin/bash

for i in {1..5}
do
    echo "This is loop iteration $i"
    if [ $i -eq 3 ]
    then
        exit
    fi
done

echo "Loop finished."

在这个示例中,当 i 等于 3 时,exit 命令将跳出循环。因此,输出将只显示前两个迭代:

This is loop iteration 1
This is loop iteration 2

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

推荐阅读:linux foreach如何跳出循环

0