Bash bug re $LINENO——或者我只是感到困惑?
考虑一下:
#!/bin/bash echo ' ' $LINENO echo '' ' ' $LINENO
第一个 echo 正确地打印了 4,但第二个 echo 打印了 5 而不是 6。我是否遗漏了某些内容,或者这是一个错误? (使用 bash 3.00.15)
Consider:
#!/bin/bash echo ' ' $LINENO echo '' ' ' $LINENO
The first echo correctly prints a 4, but the second echo prints a 5 instead of 6. Am I missing something, or is this a bug? (Using bash 3.00.15)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它看起来像是 bash 中的一个实现错误(bug)。
我使用了:
which 产生:
它支持这样的理论:在 shell 认为该行已完成之前对变量进行评估。一旦该行完成,它就会更新 LINENO 并继续。
测试的 Bash 版本:3.2.48 (mac)、4.1.5 (linux)
当我使用语法时:
它会获取较新的行号。它似乎与作为该行唯一参数携带的空字符串的评估有关。
It looks like an implementation misfeature (bug) in bash.
I used:
which yielded:
Which supports the theory that the variable is evaluated before the shell considers the line to have been completed. Once the line has been completed, it updates the LINENO and proceeds.
Bash versions tested: 3.2.48 (mac), 4.1.5 (linux)
When I use the syntax:
it gets the newer line number. It seems to be related to the evaluation of the empty string carried as the only argument on the line.
Bash
似乎解释了多字符串 &echo
命令的多行参数仅位于源代码文件(脚本)的一行上,因为Bash
必须连接多字符串 &将echo
命令的多行参数转换为单个(一行)参数。连接机制也由空字符串''
后跟包含换行符的字符串echo -e '' + '\n' + $LINENO
触发。Bash
seems to interpret a multi-string & multi-line argument to theecho
command to be on just one line of the source code file (script) becauseBash
has to concatenate the multi-string & multi-line argument to theecho
command into a single (one line) argument. The concatenation mechanism is also triggered by an empty string''
followed by a string containing a newline characterecho -e '' + '\n' + $LINENO
.