Bash command line and input limit

前端 未结 3 756
春和景丽
春和景丽 2020-11-22 08:36

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?

I.e. Is it possible to wri

3条回答
  •  温柔的废话
    2020-11-22 09:10

    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:

    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 f in *; do :; done
    

    which is another shell builtin.

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

    And indeed, I can ls my 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 long error. So ARG_MAX is working as advertised: the exec is failing with more than ARG_MAX characters on the argument list- including, it should be noted, the environment data.

提交回复
热议问题