简体   繁体   English

减少bash环境变量的大小

[英]Reduce bash environment variables size

Let's say I have a program that takes a very large number of command line arguments or a very large single command line argument. 假设我有一个程序,它需要大量的命令行参数或非常大的单个命令行参数。 Let's assume, that my application wants to accept a single command line argument as a string of 65536 symbols (64kB). 假设我的应用程序希望接受一个命令行参数作为65536个符号(64kB)的字符串。 As everybody knows there is a limit on what amount of bytes one can pass to process with a command line and environment variables. 众所周知,使用命令行和环境变量可以传递的字节数是有限制的。

And here is a little experiment: 这是一个小实验:

$ set > t
$ ls -lF | grep "\<t\>"
-rw-r--r--  1 sergey sergey 118737 Oct 14 23:45 t

And this 118737 bytes is about 115kB. 而这118737字节约为115kB。 Header file /usr/include/linux/limits.h says that ARG_MAX is 131072 which is exactly 128kB. 头文件/usr/include/linux/limits.h表示ARG_MAX是131072,正好是ARG_MAX

Is there a way to reduce environment variables size of user bash session? 有没有办法减少用户bash会话的环境变量大小?

set | wc -c set | wc -c (which is a much simpler way to count the size of all shell variables) is not relevant, since most of those variables are not exported and the limit applies only to exported variables and command line arguments. set | wc -c (这是一种计算所有shell变量大小的简单得多的方法)无关紧要,因为这些变量中的大多数都不导出,并且限制仅适用于导出的变量和命令行参数。

Try export | wc -c 尝试export | wc -c export | wc -c to get a realistic view. export | wc -c以获得逼真的视图。 On one shell session I happen to have open: 在一个shell会话中,我碰巧已经打开了:

$ set | wc -c
241235
$ export | wc -c
4652

You can also get a variety of useful information from xargs : 您还可以从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

Or a simpler report from getconf : 或更简单的getconf报告:

$ getconf ARG_MAX
2097152

It is quite possible that your particular system allows more than ARG_MAX bytes. 您的特定系统很有可能允许超过ARG_MAX个字节。

I believe the export | wc -c 相信export | wc -c export | wc -c command is not enough if you want to use that route as an approximation. 如果要使用该路由作为近似值, export | wc -c命令是不够的。 I use bash shell functions. 我使用bash shell函数。 A LOT of them. 很多。 And I export them to be available in subshells. 然后,我将它们导出到子外壳中。 Those are only discovered with export -f . 这些仅通过export -f发现。 So in theory you really need the output of both. 因此,从理论上讲,您确实需要两者的输出。

However, looking at my machine: 但是,看着我的机器:

$  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

Perhaps the output of the export command gives a good look at the ratio between exported variables and functions, but it isn't reflective of the actual space used (9309 + 150917 != 121160) . 也许export命令的输出可以很好地了解导出的变量和函数之间的比率,但是并不能反映所使用的实际空间(9309 + 150917 != 121160)

And that kind of makes sense. 这样说是有道理的。 Here is the output of one random function in my environment, via export -f : 这是我的环境中通过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

Here is the same function plucked out of env 这里是相同的功能抠出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 (and I think type ) appear to do some prettying for presentation which inflates the wc -c value. export (我认为是type )似乎为演示做了一些修饰,使wc -c值膨胀了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM