Bash语法速查:循环语句

neevop 十二月 6, 2022

Basic for loop

for i in /etc/rc.*; do
  echo "$i"
done

C-like for loop

for ((i = 0 ; i < 100 ; i++)); do
  echo "$i"
done

Ranges

for i in {1..5}; do
    echo "Welcome $i"
done

With step size

for i in {5..50..5}; do
    echo "Welcome $i"
done

Reading lines

while read -r line; do
  echo "$line"
done <file.txt

Forever

while true; do
  ···
done