When passing arguments to scripts in Linux, there exists a fundamental system limit called ARG_MAX
defined in limits.h
. This defines the maximum combined length of command-line arguments and environment variables.
#include <stdio.h>
#include <limits.h>
int main() {
printf("ARG_MAX: %ld\n", sysconf(_SC_ARG_MAX));
return 0;
}
On modern Linux systems (kernel 2.6.23+), you'll typically find:
- Default value: 2MB (2097152 bytes)
- Minimum guaranteed value: 32KB (32768 bytes)
- Actual limit varies by system configuration
Several ways to check the limit:
# Using getconf
getconf ARG_MAX
# Checking kernel parameter
cat /proc/sys/kernel/args-max
# For per-process limit check
ulimit -s
When approaching the limit, consider these alternatives:
# 1. Use stdin redirection
echo "very_long_data_here" | myscript
# 2. Use temporary files
tmpfile=$(mktemp)
echo "very_long_data_here" > $tmpfile
myscript --input-file=$tmpfile
rm $tmpfile
# 3. Use environment variables (limited by ENV_MAX)
export LONG_VAR="very_long_data_here"
myscript
For systems requiring larger limits (e.g., big data processing):
# Temporary increase
sudo sysctl -w kernel.arg_max=4194304
# Permanent change (add to /etc/sysctl.conf)
kernel.arg_max = 4194304
- Always validate input length in scripts
- Consider argument chunking for very large data
- Document expected limits in your script's help
- Use proper error handling for boundary cases
When working with Linux command line arguments, there are actually several different limits to consider:
- ARG_MAX: Maximum total length of command line arguments
- MAX_ARG_STRLEN: Maximum length of a single argument
- MAX_ARG: Maximum number of arguments
You can check these values on your system using:
getconf ARG_MAX
getconf MAX_ARG_STRLEN
Or examine them directly:
cat /proc/sys/kernel/arg_max
xargs --show-limits
Here's a script to test argument length limits:
#!/bin/bash
# test_arg_length.sh
# Generate a long string
long_arg=$(printf '%*s' 100000 | tr ' ' 'A')
# Attempt to pass it
echo "Testing with argument length: ${#long_arg}"
./process_arg.sh "$long_arg"
When you hit these limits, consider these approaches:
# 1. Use environment variables
export LONG_VAR="your_long_string"
./script.sh
# 2. Use a file
echo "your_content" > tempfile
./script.sh @tempfile
# 3. Use xargs
printf '%s\n' "$long_var" | xargs -0 ./script.sh
Different systems may have different defaults:
- Linux: Typically 2MB total (ARG_MAX)
- MacOS: Around 256KB
- BSD Systems: Varies by version
To avoid hitting these limits:
- Use configuration files for complex setups
- Consider splitting very long arguments
- Use environment variables when appropriate
- Document your script's requirements