Setting environment variable to a large value -> “Argument list too long”

前端 未结 1 1408
走了就别回头了
走了就别回头了 2021-01-18 06:22

Inside a bash script, I set an environment variable to contain a string of 1 million characters. I do so like this:

export LG=XXXXXXX # ... 1 million X\'s


        
相关标签:
1条回答
  • 2021-01-18 07:10

    Command-line arguments and environment variables both come out of the same pool of space. Set environment variables too long, and you no longer have space for command-line arguments -- and even xargs, which breaks command line invocations down into smaller groupings to fit inside the pool where possible, can't operate when that pool is completely full.

    So: Don't do that. For instance, you might store your data in a file, and export the path to that file in the environment.


    By the way -- the reason echo works is that it's built into your shell. Thus,

    echo "$LG"
    

    ...doesn't need to start an external process, so the limits on argument list length and environment size at process startup time don't apply.

    On the other hand, if you ran

    /bin/echo "$LG"
    

    ...then you'd see the problem again.


    Given the explanation edited into the question as to what you're actually trying to accomplish, let me suggest an approach which requires neither environment space nor command-line space:

    #!/bin/bash
    #      ^-- also consider ksh; faster than bash, but also supports <()
    #          /bin/sh is not usable here, as POSIX sh does not specify <().
    
    lg=... ## DO NOT USE export HERE!
    sed -f <(printf '%s\n' "s/A/$lg/g")
    
    0 讨论(0)
提交回复
热议问题