Linux 如何在 bash for 循环中使用变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17181787/
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
How to use variables in a bash for loop
提问by Classified
How does one use a variable in a bash for loop? If I just use a standard for loop, it does what I expect
如何在 bash for 循环中使用变量?如果我只是使用标准的 for 循环,它会做我期望的
for i in {0..3}
do
echo "do some stuff $i"
done
This works fine. It loops thru 4 times, 0 to 3 inclusive, printing my message and putting the count at the end.
这工作正常。它循环 4 次,包括 0 到 3 次,打印我的消息并将计数放在最后。
do some stuff 0
do some stuff 1
do some stuff 2
do some stuff 3
When I try the same thing with the following for loop, it seems to equal a string, which is not what i want.
当我用下面的 for 循环尝试同样的事情时,它似乎等于一个字符串,这不是我想要的。
length=3
for i in {0..$length}
do
echo "do something right $i"
done
output:
输出:
do something right {0..3}
I've tried
我试过了
for i in {0.."$length"} and for i in {0..${length}} (both output was {0..3})
and
和
for i in {0..'$length'} (output was {0..$length})
and they both don't do what I need. Hopefully someone can help me. Thanks in advance for any bash expert's help with for loops.
他们都不做我需要的。希望有人可以帮助我。预先感谢任何 bash 专家对 for 循环的帮助。
采纳答案by perreal
One way is using eval
:
一种方法是使用eval
:
for i in $( eval echo {0..$length} )
do
echo "do something right $i"
done
Notewhat happens when you set length=;ls
or length=; rm *
(don't try the latter though).
注意当你设置length=;ls
or时会发生什么length=; rm *
(尽管不要尝试后者)。
safely, using seq
:
安全地,使用seq
:
for i in $( seq 0 $length )
do
echo "do something right $i"
done
or you can use the c-style for loop, which is also safe:
或者您可以使用 c 风格的 for 循环,这也是安全的:
for (( i = 0; i <= $length; i++ ))
do
echo "do something right $i"
done
回答by paxdiablo
In bash, brace expansion is the firststep attempted so, at that point, $length
will not have been substituted.
在bash中,括号扩展是第一尝试如此步骤,在该点处,$length
也不会被取代。
The manpage for bash states clearly:
bash 的联机帮助页明确指出:
A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters...
序列表达式采用 {x..y[..incr]} 形式,其中 x 和 y 是整数或单个字符...
There are a number of possibilities, such as using:
有多种可能性,例如使用:
pax> for i in $(seq 0 $length) ; do echo $i ; done
0
1
2
3
though that may give you a large command line if length
is massive.
尽管如果很大,这可能会给你一个很大的命令行length
。
Another alternative is to use the C-like syntax:
另一种选择是使用类似 C 的语法:
pax> for (( i = 0; i <= $length; i++ )) ; do echo $i; done
0
1
2
3
回答by nemo
Brace subtitutions are performed before any other, so you need to use eval
or
a third-party tool like seq
.
大括号替换在任何其他替换之前执行,因此您需要使用eval
或第三方工具,如seq
.
Example for eval:
评估示例:
for i in `eval echo {0..$length}`; do echo $i; done
This information can actually be found in man bash
:
这些信息实际上可以在man bash
以下位置找到:
A sequence expression takes the form {x..y[..incr]}, where x and y are either integers or single characters, and incr, an optional increment, is an integer. [...]
Brace expansion is performed before any other expansions, and any characters special to other expansions are preserved in the result.It is strictly textual. Bash does not apply any syntactic interpretation to the context of the expansion or the text between the braces.
序列表达式采用 {x..y[..incr]} 形式,其中 x 和 y 是整数或单个字符,而 incr 是一个可选增量,是一个整数。[...]
大括号扩展在任何其他扩展之前执行,并且任何其他扩展所特有的字符都保留在结果中。它是严格的文本。Bash 不对扩展的上下文或大括号之间的文本应用任何句法解释。