bash seq 替换中的变量 ({1..10})
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6191146/
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
Variables in bash seq replacement ({1..10})
提问by Tyilo
Is it possible to do something like this:
是否有可能做这样的事情:
start=1
end=10
echo {$start..$end}
# Ouput: {1..10}
# Expected: 1 2 3 ... 10 (echo {1..10})
回答by anubhava
In bash, brace expansion happens before variable expansion, so this is not directly possible.
在 bash 中,大括号扩展发生在变量扩展之前,所以这不是直接可能的。
Instead, use an arithmetic for
loop:
相反,使用算术for
循环:
start=1
end=10
for ((i=start; i<=end; i++))
do
echo "i: $i"
done
OUTPUT
输出
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
i: 10
回答by cnicutar
You should consider using seq(1)
. You can also use eval:
您应该考虑使用seq(1)
. 您还可以使用 eval:
eval echo {$start..$end}
And here is seq
这里是 seq
seq -s' ' $start $end
回答by bmk
You have to use eval
:
你必须使用eval
:
eval echo {$start..$end}
回答by glenn Hymanman
If you don't have seq
, you might want to stick with a plain for loop
如果你没有seq
,你可能想坚持一个普通的 for 循环
for (( i=start; i<=end; i++ )); do printf "%d " $i; done; echo ""
回答by Spencer Rathbun
Are you positive it has be BASH? ZSHhandles this the way you want. This won't work in BASH because brace expansion happens before any other expansion type, such as variable expansion. So you will need to use an alternative method.
你确定它是 BASH 吗?ZSH以您想要的方式处理此问题。这在 BASH 中不起作用,因为大括号扩展发生在任何其他扩展类型之前,例如变量扩展。因此,您将需要使用替代方法。
Any particular reason you need to combine brace and variable expansion? Perhaps a different approach to your problem will obviate the need for this.
您需要结合大括号和变量扩展的任何特殊原因?也许对您的问题采取不同的方法将消除这种需要。
回答by opsguy
I normally just do this:
我通常只是这样做:
echo `seq $start $end`
回答by lantian0811
use -s ex: seq -s ' ' 1 10 output: 1 2 3 4 5 6 7 8 9 10
使用 -s 例如:seq -s ' ' 1 10 输出:1 2 3 4 5 6 7 8 9 10