这是 myscript.sh
#!/bin/bash
for i in {1..$1};
do
echo $1 $i;
done
如果我运行myscript.sh 3
输出是
3 {1..3}
代替
3 1
3 2
3 3
显然$3
包含正确的值,那么为什么不像for i in {1..$1}
我for i in {1..3}
直接写的那样表现呢?
这是 myscript.sh
#!/bin/bash
for i in {1..$1};
do
echo $1 $i;
done
如果我运行myscript.sh 3
输出是
3 {1..3}
代替
3 1
3 2
3 3
显然$3
包含正确的值,那么为什么不像for i in {1..$1}
我for i in {1..3}
直接写的那样表现呢?
您应该使用 C 风格的 for 循环来完成此操作:
for ((i=1; i<=$1; i++)); do
echo $i
done
这避免了外部命令和讨厌的 eval 语句。
因为大括号扩展发生在变量扩展之前。http://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion。
如果你想使用大括号,你可以像这样严峻:
for i in `eval echo {1..$1}`;
do
echo $1 $i;
done
摘要:Bash 是邪恶的。
您可以使用seq
命令:
for i in `seq 1 $1`
或者你可以使用 C 风格for...loop
:
for((i=1;i<=$1;i++))
Here is a way to expand variables inside braces without eval:
end=3
declare -a 'range=({'"1..$end"'})'
We now have a nice array of numbers:
for i in ${range[@]};do echo $i;done
1
2
3
I know you've mentioned bash in the heading, but I would add that 'for i in {$1..$2}' works as intended in zsh. If your system has zsh installed, you can just change your shebang to zsh.
Using zsh with the example 'for i in {$1..$2}' also has the added benefit that $1 can be less than $2 and it still works, something that would require quite a bit of messing about if you wanted that kind of flexibility with a C-style for loop.