Bash 命令行和输入限制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19354870/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:19:19  来源:igfitidea点击:

Bash command line and input limit

bashshellunixcommand-line-arguments

提问by Derek Halden

Is there some sort of character limit imposed in bash (or other shells) for how long an input can be? If so, what is that character limit?

bash(或其他shell)中是否对输入施加了某种字符限制?如果是这样,字符限制是多少?

I.e. Is it possible to write a command in bash that is too long for the command line to execute? If there is not a required limit, is there a suggested limit?

即是否可以在 bash 中编写一个命令行执行太长的命令?如果没有要求的限制,是否有建议的限制?

回答by Jens

The limit for the length of a command line is not imposed by the shell, but by the operating system. This limit is usually in the range of hundred kilobytes. POSIX denotes this limit ARG_MAXand on POSIX conformant systems you can query it with

命令行长度的限制不是由 shell 强加的,而是由操作系统强加的。此限制通常在数百 KB 的范围内。POSIX 表示此限制ARG_MAX,在符合 POSIX 的系统上,您可以使用

$ getconf ARG_MAX    # Get argument limit in bytes

E.g. on Cygwin this is 32000, and on the different BSDs and Linux systems I use it is anywhere from 131072 to 2621440.

例如,在 Cygwin 上这是 32000,而在我使用的不同 BSD 和 Linux 系统上,它是从 131072 到 2621440 的任何地方。

If you need to process a list of files exceeding this limit, you might want to look at the xargsutility, which calls a program repeatedly with a subset of arguments not exceeding ARG_MAX.

如果您需要处理超出此限制的文件列表,您可能需要查看该xargs实用程序,该实用程序使用不超过ARG_MAX.

To answer your specific question, yes, it is possible to attempt to run a command with too long an argument list. The shell will error with a message along "argument list too long".

要回答您的具体问题,是的,可以尝试运行参数列表过长的命令。shell 会出错,并显示“参数列表太长”的消息。

Note that the inputto a program (as read on stdin or any other file descriptor) is notlimited (only by available program resources). So if your shell script reads a string into a variable, you are not restricted by ARG_MAX. The restriction also does not apply to shell-builtins.

注意,输入到一个程序(如读取标准输入或任何其他文件描述符)是受限(仅受可用程序资源)。因此,如果您的 shell 脚本将字符串读入变量,则您不受ARG_MAX. 该限制也不适用于 shell 内置函数。

回答by Mike S

Ok, Denizens. So I have accepted the command line length limits as gospel for quite some time. So, what to do with one's assumptions? Naturally- check them.

好的,居民们。所以我已经接受命令行长度限制作为福音很长一段时间了。那么,如何处理一个人的假设呢?自然地-检查它们。

I have a Fedora 22 machine at my disposal (meaning: Linux with bash4). I have created a directory with 500,000 inodes (files) in it each of 18 characters long. The command line length is 9,500,000 characters. Created thus:

我有一台 Fedora 22 机器可供我使用(意思是:带有 bash4 的 Linux)。我创建了一个目录,其中包含 500,000 个 inode(文件),每个目录长 18 个字符。命令行长度为 9,500,000 个字符。如此创建:

seq 1 500000 | while read digit; do
    touch $(printf "abigfilename%06d\n" $digit);
done

And we note:

我们注意到:

$ getconf ARG_MAX
2097152

Note however I can do this:

但是请注意,我可以这样做:

$ echo * > /dev/null

But this fails:

但这失败了:

$ /bin/echo * > /dev/null
bash: /bin/echo: Argument list too long

I can run a for loop:

我可以运行一个 for 循环:

$ for f in *; do :; done

which is another shell builtin.

这是另一个内置的shell。

Careful reading of the documentation for ARG_MAXstates, Maximum length of argument to the exec functions. This means: Without calling exec, there is no ARG_MAXlimitation. So it would explain why shell builtins are not restricted by ARG_MAX.

仔细阅读状态文档ARG_MAX最大长度参数到 exec 函数。这意味着:没有调用exec,就没有ARG_MAX限制。所以它可以解释为什么 shell 内置函数不受ARG_MAX.

And indeed, I can lsmy directory if my argument list is 109948 files long, or about 2,089,000 characters (give or take). Once I add one more 18-character filename file, though, then I get an Argument list too longerror. So ARG_MAXis working as advertised: the exec is failing with more than ARG_MAXcharacters on the argument list- including, it should be noted, the environment data.

事实上,ls如果我的参数列表长度为 109948 个文件,或者大约 2,089,000 个字符(给予或接受),我可以使用我的目录。但是,一旦我再添加一个 18 个字符的文件名文件,就会收到一个Argument list too long错误。所以ARG_MAX正如宣传的那样工作:exec 失败,ARG_MAX参数列表中的字符数超过了 - 包括,应该注意的是,环境数据。

回答by Paul Kenjora

There is a buffer limit of something like 1024. The read will simply hang mid paste or input. To solve this use the -e option.

有一个类似 1024 的缓冲区限制。读取将简单地挂在中间粘贴或输入。要解决此问题,请使用 -e 选项。

http://linuxcommand.org/lc3_man_pages/readh.html

http://linuxcommand.org/lc3_man_pages/readh.html

-e use Readline to obtain the line in an interactive shell

-e 使用 Readline 获取交互式 shell 中的行

Change your read to read -e and annoying line input hang goes away.

将您的 read 更改为 read -e 并且烦人的行输入挂起消失了。