假设我有一个程序需要大量的命令行参数或一个非常大的单个命令行参数。我们假设,我的应用程序想要接受单个命令行参数作为65536个符号(64kB)的字符串。众所周知,对于使用命令行和环境变量处理的字节数有限制。
这是一个小实验:
$ set > t
$ ls -lF | grep "\<t\>"
-rw-r--r-- 1 sergey sergey 118737 Oct 14 23:45 t
这个118737字节大约是115kB。标头文件/usr/include/linux/limits.h
表示ARG_MAX
是131072,正好是128kB。
有没有办法减少用户bash会话的环境变量大小?
答案 0 :(得分:4)
\"
(这是一种计算所有shell变量大小的简单方法)并不重要,因为大多数变量都不会导出,并且限制仅适用于导出的变量和命令行参数。 / p>
尝试set | wc -c
以获得逼真的视图。在一个shell会话中,我碰巧打开了:
export | wc -c
您还可以从$ set | wc -c
241235
$ export | wc -c
4652
获取各种有用的信息:
xargs
来自$ xargs --show-limits </dev/null
Your environment variables take up 3730 bytes
POSIX upper limit on argument length (this system): 2091374
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 2087644
Size of command buffer we are actually using: 131072
的简单报告:
getconf
您的特定系统很可能允许超过$ getconf ARG_MAX
2097152
个字节。
答案 1 :(得分:1)
如果您想将该路由用作近似值,我认为export | wc -c
命令是不够的。我使用bash shell函数。他们很多。我将它们导出到子壳中。只有export -f
才会发现这些内容。所以理论上你真的需要两者的输出。
然而,看着我的机器:
$ export -p | wc && export -f | wc
147 479 9309
6940 17029 150917
$ xargs --show-limits </dev/null
Your environment variables take up 121160 bytes
POSIX upper limit on argument length (this system): 1973944
POSIX smallest allowable upper limit on argument length (all systems): 4096
Maximum length of command we could actually use: 1852784
Size of command buffer we are actually using: 131072
或许export
命令的输出可以很好地了解导出的变量和函数之间的比率,但它并不反映实际使用的空间(9309 + 150917 != 121160)
。
那种有道理。以下是我的环境中一个随机函数的输出,通过export -f
:
usage_init ()
{
USAGE_OPT_P1="-p pathvar - The envvar to process. Default is";
USAGE_OPT_P2=" PATH, if -p is not specified.";
USAGE_OPT_P3=" The '-p' is optional, in which case";
USAGE_OPT_P4F=" the first ARGUMENT is assumed to be";
USAGE_OPT_P4O=" the ARGUMENT is assumed to be";
USAGE_OPT_P5=" the pathvar.";
USAGE_OPT_S="-s sep - Path element separator. Defaults to ':'.";
USAGE_OPT_X="-x - Exports 'pathvar'.";
USAGE_OPT_V="-v - Executes function 'listpath' after assignment.";
USAGE_OPT_H="-h - Gives usage message.";
USAGE_OPT_E="-e - 'pathspec' is interpreted as an egrep regexp.";
USAGE_OPT_N="-n - Add index numbers to the results."
}
declare -fx usage_init
以下是从env
BASH_FUNC_usage_init%%=() { USAGE_OPT_P1="-p pathvar - The envvar to process. Default is";
USAGE_OPT_P2=" PATH, if -p is not specified.";
USAGE_OPT_P3=" The '-p' is optional, in which case";
USAGE_OPT_P4F=" the first ARGUMENT is assumed to be";
USAGE_OPT_P4O=" the ARGUMENT is assumed to be";
USAGE_OPT_P5=" the pathvar.";
USAGE_OPT_S="-s sep - Path element separator. Defaults to ':'.";
USAGE_OPT_X="-x - Exports 'pathvar'.";
USAGE_OPT_V="-v - Executes function 'listpath' after assignment.";
USAGE_OPT_H="-h - Gives usage message.";
USAGE_OPT_E="-e - 'pathspec' is interpreted as an egrep regexp.";
USAGE_OPT_N="-n - Add index numbers to the results."
}
export
(我认为type
)似乎做了一些非常精彩的演示文稿,夸大了wc -c
值。