Path Variable Limit

Is there a character limit on how long your PATH variable can be on macOS? Or is there a limit in terms of the amount of binaries you can reference in your path?


Solution 1:

The size of a variable is subject to the limitations of the hardware on which is it run, that said, a variable could be milllions of bytes. But an environment variable,such as PATH, is subject to system limitations (ARG_MAX) since the whole environment is passed to the system call- execve. So, a reasonable estimate of the maximum size of an environment variable would be, the bytes used in the current environment- env | wc -c, plus a reasonable buffer of bytes needed to run commands in the shell- 2048 bytes to 4096 bytes, subtracted from the maximum number of bytes allowed in an argument- 262144 bytes. Usablility of an environment variable this size is another issue.

The shell starts its search in the left most path listed in the PATH variable then continues to the next and next until it finds the command that you issued. The shell also keeps a record of that command so it does not need to search the PATH variable again in the current session. This is an indication that searching PATH is expensive. So, the maximum size of your PATH variable should be as small as you can make it.

Solution 2:

You'll basically never hit the limit if you have any sort of reasonably sane PATH.

I have just tried having a PATH variable that is 96 kB long (unreasonably long). It works on High Sierra without problems.

If I step it up to 128 kB, I begin having problems launching commands as they will fail due to the large environment. The PATH environment is stored though.