Posts Tagged ‘4096 limit’

4k input limit in terminal

January 23, 2017 Leave a comment

Today I ran into a strange problem. Take this code:

s = input("text> ")

If the input is very long, then it is truncated to 4096 characters (I tried it under Linux). The same happens when you do “cat | wc” and paste in a long string. What???

It turns out that there’s a 4k kernel line length limit on terminal input (link). But how to overcome this problem?

0) Well, probably the best way is not to insert such a long string in the terminal. Pass it through a pipe (“cat long.txt | wc” does work) or read it from a file.

But, if you really want to paste in a long string, here is what you can do:

1) With the command “stty -icanon” you can disable the canonical mode. Paste in the string, and then I think it’s a good idea to enable the canonical mode again with “stty icanon” (link).

2) Under Python I found a simple way. Just “import readline” and it solved the issue for me. I tried it with a 11,000 characters long string and it worked.

Thanks to #python on IRC for helping to solve this issue.

Categories: python Tags: , ,