#!/bin/bash
for varibale1 in 1 2 3 4 5 6
do
echo "Hello,Welcome $varibale1 times"
done
#!/bin/bash
for variable1 in {1..5}
do
echo "Hello,Welcome $variable1 times"
done
结果:
~/Note/test # ./6for2.sh
Hello,Welcome 1 times
Hello,Welcome 2 times
Hello,Welcome 3 times
Hello,Welcome 4 times
Hello,Welcome 5 times
例子2:
求100内的奇数和
#!/bin/bash
for i in {1..100..2}
do
let "sum+=i"
done
echo "The sum is $sum"
结果:
~/Note/test # ./7for.sh
The sum is 2500
例子3:
没有列表的for语句,这种情况一般就是命令行参数给列表
#!/bin/bash
echo "number of argument is $#"
echo "what you input is:"
for arguemnt
do
echo "$arguemnt"
done
结果:
~/Note/test # ./8for_no_list.sh
number of argument is 0
what you input is:
~/Note/test # ./8for_no_list.sh 1 2 3
number of argument is 3
what you input is:
1
2
3
例子4:c语言格式的for语句
#!/bin/bash
for(( i = 1; i <= 5; i++ ))
do
echo "$i"
done
#!/bin/bash
for(( i = 1;i <= 100; i += 2))
do
let "sum += i"
done
echo "sum = $sum"