Bash 命令行和输入限制 [英] Bash command line and input limit

查看:41
本文介绍了Bash 命令行和输入限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 中编写一个对于命令行执行来说太长的命令?如果没有要求的限制,是否有建议的限制?

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?

推荐答案

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

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_MAX and on POSIX conformant systems you can query it with

$ getconf ARG_MAX    # Get argument limit in bytes

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

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

如果您需要处理超出此限制的文件列表,您可能需要查看 xargs 实用程序,该实用程序使用不超过 ARG_MAX<的参数子集重复调用程序/代码>.

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

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

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".

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

Note that the input to a program (as read on stdin or any other file descriptor) is not limited (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.

这篇关于Bash 命令行和输入限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆