shell for 循环怎么写
在bash shell中,for
循环的基本语法如下:
for variable in 1 2 3...n
do
command1
command2
...
commandN
done
其中,“variable”是循环变量,“1 2 3...n”是值序列,“command1, command2, ... commandN”是每次循环要执行的命令。
举个例子,让我们使用一个简单的for
循环来打印数字1到5:
for i in 1 2 3 4 5
do
echo "Number is $i"
done
这将输出:
Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
另外,如果你想遍历一个较大范围的数字,你可以使用下面的语法:
for i in {1..10}
do
echo "Number is $i"
done
这会打印从1到10的数字。