Here-docs 的 bash shell 长度限制是什么?

Here-docs 的 bash shell 长度限制是什么?

bash使用此处文档作为命令行的一部分有哪些长度限制?我发现简短的here-docs 工作得很好,但是当它们变得更长时,就会出现一些点或结构,然后它们就会中断。当失败时,有两个症状。一是here-doc很早就被放弃了。另一个是,here-doc 中的一行或多行在输出中被截断,并且不一定是最后一行。

下面您将看到一个人为的示例命令,我可以用它来重现该问题。这里的文档是无聊的 58 行,每行包含 79 个“x”字符和一个换行符。如果我复制并粘贴所有 60 行,不是EOF包括从, 到交互式shell 的换行符bash,那么我必须手动点击Enter,整个命令每次都会成功运行。但是,如果我复制并粘贴所有 60 行,包括换行符EOF,那么这就是发生故障的时候。如果我在桌面上的终端应用程序中执行后者,它总是失败。如果我在远程 SSH 客户端中执行此操作,有时会成功,有时会失败。显着减少here-doc行的数量,问题就消失了。放弃此处文档的一部分意味着所有剩余的此处文档行EOF行被视为bash新的单独命令行,这只会导致一系列“未找到命令”错误(如果此处文档包含合法命令行,则情况可能会更糟)。

cat > ~/test.txt << 'EOF'
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOF

我知道bash有命令行长度限制,但这个示例远低于这些限制。事实上,上面的多行命令中没有一行的长度超过 80 个字符,并且每一行都会从 shell 获得自己的提示符,因此我什至不确定会应用什么限制。对我来说,这里文档的长度应该受到限制,这似乎很奇怪。

我正在使用 Linux Mint 17 (Ubuntu 14.04)。

$ bash --version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
...
$ uname -a
Linux QWERTY 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

当在 bash 脚本中使用此类命令时,不会出现此问题。

答案1

我不知道这里文档有任何大小限制。我正在运行kernel 3.9.1,并且在这里遇到了同样的问题:在终端中粘贴大块文本时,某些行被截断或丢失。我发现(经过一番谷歌搜索后),如果您关闭行编辑,粘贴效果很好(此处讨论:将大量文本粘贴到启用读取行的程序中会截断所粘贴文本的部分行)。
进一步的调查表明,这个问题的根源实际上是一个内核错误(drivers/tty),显然它是已在内核 >= 3.14 中修复。这可能解释了为什么安装了更多最新版本的人无法复制我们都经历过的行为。
更新:
我可以确认安装后kernel 3.16.6一切正常,因此修补内核或升级到更新的版本(>=3.14)应该可以解决此问题。

相关内容