阅读背景:

在{1.. .$1}不能正常工作

来源:互联网 

Here is myscript.sh

这是myscript.sh

#!/bin/bash
for i in {1..

Here is myscript.sh

这是myscript.sh

#!/bin/bash
for i in {1..$1};
do
    echo $1 $i;
done

If I run myscript.sh 3 the output is

如果我myscript运行。输出是。

3 {1..3}

instead of

而不是

3 1
3 2
3 3

Clearly $3 contains the right value, so why doesn't for i in {1..$1} behave the same as if I had written for i in {1..3} directly?

显然$3包含正确的值,所以为什么不在{1}中为i。$1}的行为与我在{1}中为I所写的一样。直接3 } ?

5 个解决方案

#1


50  

You should use a C-style for loop to accomplish this:

您应该使用C-style for循环来实现以下目标:

for ((i=1; i<=$1; i++)); do
   echo $i
done

This avoids external commands and nasty eval statements.

这避免了外部命令和讨厌的eval语句。

#2


23  

Because brace expansion occurs before expansion of variables. https://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion.

因为支撑展开发生在变量展开之前。https://www.gnu.org/software/bash/manual/bashref.html Brace-Expansion。

If you want to use braces, you could so something grim like this:

如果你想用牙套,你可以做一些像这样可怕的事情:

for i in `eval echo {1..$1}`;
do
    echo $1 $i;
done

Summary: Bash is vile.

摘要:Bash是可耻的。

#3


11  

You can use seq command:

您可以使用seq命令:

for i in `seq 1 $1`

Or you can use the C-style for...loop:

或者你也可以使用C-style for…

for((i=1;i<=$1;i++))

#4


1  

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.

我知道你在标题中提到了bash,但是我要在{$1}中加上它。$2}在zsh中的工作。如果您的系统已经安装了zsh,您可以将shebang更改为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.

使用zsh和示例“for i in{$1..”$2}也有额外的好处,1美元可以少于2美元,而且它仍然有效,如果你想要那种带有c型循环的灵活性,那就需要相当多的麻烦。

#5


1  

Here is a way to expand variables inside braces without eval:

这里有一种方法可以在不使用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

}; do echo

Here is myscript.sh

这是myscript.sh

#!/bin/bash
for i in {1..$1};
do
    echo $1 $i;
done

If I run myscript.sh 3 the output is

如果我myscript运行。输出是。

3 {1..3}

instead of

而不是

3 1
3 2
3 3

Clearly $3 contains the right value, so why doesn't for i in {1..$1} behave the same as if I had written for i in {1..3} directly?

显然$3包含正确的值,所以为什么不在{1}中为i。$1}的行为与我在{1}中为I所写的一样。直接3 } ?

5 个解决方案

#1


50  

You should use a C-style for loop to accomplish this:

您应该使用C-style for循环来实现以下目标:

for ((i=1; i<=$1; i++)); do
   echo $i
done

This avoids external commands and nasty eval statements.

这避免了外部命令和讨厌的eval语句。

#2


23  

Because brace expansion occurs before expansion of variables. https://www.gnu.org/software/bash/manual/bashref.html#Brace-Expansion.

因为支撑展开发生在变量展开之前。https://www.gnu.org/software/bash/manual/bashref.html Brace-Expansion。

If you want to use braces, you could so something grim like this:

如果你想用牙套,你可以做一些像这样可怕的事情:

for i in `eval echo {1..$1}`;
do
    echo $1 $i;
done

Summary: Bash is vile.

摘要:Bash是可耻的。

#3


11  

You can use seq command:

您可以使用seq命令:

for i in `seq 1 $1`

Or you can use the C-style for...loop:

或者你也可以使用C-style for…

for((i=1;i<=$1;i++))

#4


1  

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.

我知道你在标题中提到了bash,但是我要在{$1}中加上它。$2}在zsh中的工作。如果您的系统已经安装了zsh,您可以将shebang更改为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.

使用zsh和示例“for i in{$1..”$2}也有额外的好处,1美元可以少于2美元,而且它仍然有效,如果你想要那种带有c型循环的灵活性,那就需要相当多的麻烦。

#5


1  

Here is a way to expand variables inside braces without eval:

这里有一种方法可以在不使用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; done #!/bin/ba



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: