what is the max number of characters 'read'

2019-06-07 19:16发布

I have the following shell script to read lines from terminal

#!/bin/bash

while read line
do
if [ -z ${line} ]
then
    break
fi
echo ${line}
done

I cannot enter more than 256 characters. The terminal doesn't allow me to do so (Terminal doesn't print anything beyond 256 characters, not even new line. only thing it allows is backspace)

$ ./echo.sh
wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

Im using solaris 10 OS terminal used is putty bash version 3.2.52

Is this a limitation in bash, or putty? I know to break my input into several lines, but I need to know if there is way to overcome maximum number of characters entered for read command as input

标签: bash shell unix
1条回答
神经病院院长
2楼-- · 2019-06-07 19:39

This is limitation in the OS terminal drivers.

We normally take it completely for granted, but when you enter hi<Backspace>ello and press enter, the process just reads hello.

The terminal driver makes this happen by storing the line in memory, and then deleting the last character in that buffer when you press Backspace. The buffer used for this has some human scale size. On Linux it's 4096 bytes, for comparison.

You can get around this by disabling line editing:

stty -icanon

read will now read more data at a time, but if you press Backspace, the application will actually read a backspace character instead of the final line.

查看更多
登录 后发表回答