bash的for循环:一个数字范围 [英] bash for loop: a range of numbers
本文介绍了bash的for循环:一个数字范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在sh文件以下code:
I have the following code in an .sh file:
for num in {1..10}
do
echo $num
done
这应打印数字1到10。但是,这是我得到:
Which should print numbers from 1 to 10. But, this is what I get:
{1..10}
此外,使用类似C的语法来不工作,以及:
Also, using C-like sytax doesn't work as well:
for ((i=1; i<=10; i++))
这让我的错误:
Syntax error: Bad for loop variable
庆典的版本,我已经是4.2.25。
The version of bash that I have is 4.2.25.
推荐答案
在code应该如下(注意家当说庆典
,而不是 SH
)
The code should be as follows (note the shebang says bash
, not sh
):
#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..1}
do
echo "Welcome $i times"
done
源<一个href=\"http://www.cyberciti.biz/faq/bash-for-loop/\">http://www.cyberciti.biz/faq/bash-for-loop/
这篇关于bash的for循环:一个数字范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文