bash for 循环:一系列数字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17752902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bash for loop: a range of numbers
提问by sodiumnitrate
I have the following code in an .sh file:
我在 .sh 文件中有以下代码:
for num in {1..10}
do
echo $num
done
Which should print numbers from 1 to 10. But, this is what I get:
应该打印从 1 到 10 的数字。但是,这就是我得到的:
{1..10}
Also, using C-like sytax doesn't work as well:
此外,使用类似 C 的 sytax 也不起作用:
for ((i=1; i<=10; i++))
This gets me an error:
这让我出错:
Syntax error: Bad for loop variable
The version of bash that I have is 4.2.25.
我的 bash 版本是 4.2.25。
回答by Pradheep
The code should be as follows (note the shebang says bash
, not sh
):
代码应该如下(注意shebang说的是bash
,不是sh
):
#!/bin/bash
echo "Bash version ${BASH_VERSION}..."
for i in {0..10..1}
do
echo "Welcome $i times"
done