为什么 bash 忽略 '-'在序列大括号扩展中?
这会产生 1 到 10 之间的所有数字
echo {1..10}
这个会产生 1 到 10 之间的所有奇数(增量/步长值为 2)
echo {1..10..2}
我尝试了一下,结果发现如果我在增量前面加上 -
标志,它没有效果
echo {1..10..-2}
为什么这是接受的,而不是一个错误?
This yields all numbers between 1 and 10
echo {1..10}
This one yields all odd numbers between 1 and 10 (increment/step value is 2)
echo {1..10..2}
I experimented a bit, and it turned out if I prefix the increment by a -
sign, it has no effect
echo {1..10..-2}
Why is this accepted, rather than being an error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从
bash(1)
手册页:所以...技术上输出确实每一项之间的差异为-2。但你仍然告诉它在序列中递增而不是递减。
From the
bash(1)
man page:So... technically the output does have a difference of -2 between each term. But you still told it to increment rather than decrement in the sequence.
Bash 4.1 的实验(与我正在使用的机器上默认安装的 3.2 版本相反,它不会将符号识别为特殊符号)显示:
因此,似乎递增的方向是由前两个数字控制的;增量的大小由第三个控制(如果第三个缺失则默认为 1)。
Experimentation with Bash 4.1 (as opposed to the 3.2 version installed by default on the machine I'm using, which does not recognize the notation as special) shows:
So, it seems that the direction of the incrementing is controlled by the first two numbers; the magnitude of the incrementing is controlled by the third (defaulting to 1 if the third is missing).