LJS
LJS

Reputation: 327

Is there a maximum number of characters that can be passed within one positional parameter in bash?

I have a a variable which is a list (can have 10.000 characters) which I want to send as one positional parameter when calling some script. So I wonder is there a maximum number of characters that can be send this way?

The other option would be to save it in some file that will be read by this script, but from some reasons, first scenario may be easier for implementation.

It is RedHat OS.

Thanks.

Upvotes: 0

Views: 713

Answers (2)

user1934428
user1934428

Reputation: 22217

There is a built-in limit to the operating system, on how much data can be passed to a child process. You can query this value from the command line using

getconf ARG_MAX

This value comprises the total length over all arguments plus the environment. There is no additional restriction for a single argument, but it means that if you have an unreasonably large environment, and want to pass many arguments to a process, this reduces the allowed size of a single argument.

In addition, there seems to be a maximum limit for a single argument, but this value can't be queried by getconf directly. However, there exists the configuration value for PAGE_SIZE, and it is until now a common convention to guarantee a argument size of at least 32 times this value. Hence a heuristic calculation for this limit would be

printf %d $((32 * $(getconf PAGE_SIZE) ))

Upvotes: 3

jhnc
jhnc

Reputation: 16752

On modern linux, the maximum length of a single argument is MAX_ARG_STRLEN which is probably 131072 bytes:

/usr/include/linux/binfmts.h:

#define MAX_ARG_STRLEN (PAGE_SIZE * 32)

/usr/include/x86_64-linux-gnu/sys/user.h:

#define PAGE_SHIFT              12
#define PAGE_SIZE               (1UL << PAGE_SHIFT)

See also:

Upvotes: 4

Related Questions