4095 is the limit of the tty line discipline internal editor length on Linux. From the termios(3)
man page:
- The maximum line length is 4096 chars (including the terminating newline character); lines longer than 4096 chars are truncated. After 4095 characters, input processing (e.g., ISIG and ECHO* processing) continues, but any input data after 4095 characters up to (but
not including) any terminating newline is discarded. This ensures that the terminal can always receive more input until at least one
line can be read.
See also the corresponding code in the Linux kernel.
For instance, if you enter:
$ wc -cEnter
Enter in the shell's own line editor (readline in the case of bash) submits the line to the shell. As the command line is complete, the shell is ready to execute it, so it leaves its own line editor, puts the terminal device back in canonical (aka cooked) mode, which enables that crude line editor (actually implemented in tty driver in the kernel).
Then, if you paste a 5000 byte line, press Ctrl+D to submit that line, and once again to tell wc
you're done, you'll see 4095
as output.
(Note that that limit does not apply to bash
's own line editor, you'll see you can paste a lot more data at the prompt of the bash
shell).
So if your receiving application reads lines of input from its stdin and its stdin is a terminal device and that application doesn't implement its own line editor (like bash
does) and doesn't change the input mode, you won't be able to enter lines longer than 4096 bytes (including the terminating newline character).
You could however disable the line editor of the terminal device (with stty -icanon
) before you start that receiving application so it reads input directly as you enter it. But then you won't be able to use Backspace / Ctrl + W for instance to edit input nor Ctrl + D to end the input.
If you enter:
$ saved=$(stty -g); stty -icanon icrnl; head -n1 | wc -c; stty "$saved"Enter
paste your 5000 byte long line and press Enter, you'll see 5001.
exec
is rarely needed when running a program from a script.