Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
这段代码...
#!/bin/bash cond=10; for i in {1..$cond} do echo hello; done
...简直让我发疯。这仅打印一个“你好”,i如{1..10}.
i
{1..10}
#!/bin/bash cond=10; for i in {1..10} do echo hello; done
打印 10x hello,这是需要的。如何将变量放入条件中?我尝试了不同的方法,但都没有奏效。多么容易的任务啊..提前谢谢你。
这将起作用:
cond=10; for ((i=0;i<=$cond;i++)); do echo hello; done
除了已经回答的经典循环之外,您还可以使用一些魔法:
#!/bin/bash cond=10 for i in $(eval "echo {1..$cond}") do echo hello done
但是,当然,更难阅读。