bash。为什么“ while”条件不正确的声明有效吗?
while ['qwe'='rty']
do
echo yes
done
这种情况显然是不正确的,但是终端是无限期地打印“是”。 为什么会发生?
while ['qwe'='rty']
do
echo yes
done
The condition is obviously incorrect, yet terminal is printing "yes" indefinitely.
Why is that happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,它确实不是不是 (1)。
在条件中需要空格(包括包括“命令”空间,将
[
and]
字符与条件分开(2) ))对于bash
将条件解释为三部分测试。您提供的内容(假设上面描述的缺失命令空间是简单的错别字)被视为一部分测试,只要它不是空字符串,它是正确的。
您可以看到下面的区别:
bash
man-page的相关部分具有此内容(请注意 spaces 在等式检查中,它们不仅是为在人页本身):换句话说,您的应该作为
,而
语句是:(1),从技术上讲,它是 is 不正确,但不是从您的意思(错误)中。相反,这是不正确的,因为它不是您的表达式 think 它是:-)
(2)没有这些命令空间,您会看到沿着该命令的错误线的行:
当然,除非您实际上 可执行的目标称为
[abc = xyz]
在这种情况下,它将被执行,返回值将决定哪种操作是拍摄。这是一个不可能具有可执行性的目标,但它是可能的:No, it really isn't(1).
Spaces are required in the conditional (including "command" spaces separating the
[
and]
characters from the condition by the way(2)) forbash
to interpret the condition as a three-part test.What you have provided (assuming your missing command spaces described above are a simple typo) is treated as a one-part test which is true so long as it is not an empty string.
You can see the difference below:
The relevant section of the
bash
man-page has this (note the spaces in the equality checks, they're not just for separating tokens in the man-page itself):In other words, what you should have as the
while
statement is:(1) Well, technically, it is incorrect, but not in the sense you mean (false). Instead, it's incorrect in the sense that it's not the expression you think it is :-)
(2) Without those command spaces, you'll see a syntax error along the lines of:
Unless, of course, you actually have an executable target called
[abc=xyz]
in which case it will be executed and the return value will decide what action is taken. This is an unlikely executable target to have but it is possible: