According to the manual for read
(and since it is a builtin, not man read
, but help read
):
-N nchars
Return only after reading exactly NCHARS characters, unless EOF is encountered or read times out, ignoring any delimiter
However, even if the -N
flag is used, read
still seems to want to strip out the delimeter from input data. Take the following data as an example:
# readfail.sh - Reads 5 characters at a time, and adds "RegEx style" start and end characters for clarity
while read -r -N 5 data; do
echo "^${data}\$"
done;
echo "^${data}\$" # There will be data left over just because of the way `read` uses exit codes
And here is the output:
$ echo -n "Lorem ipsum dolor sit amet" | ./readfail.sh
^Lorem$
^ipsu$
^m dol$
^or si$
^t ame$
^t$
Notice the second line is only 4 characters long. That is because there is supposed to be a space between Lorem
and ipsum
, resulting in the following five-character output: ^ ipsu$
I can fix this problem by setting IFS=
before the loop, however, isn't read
supposed to be ignoring any delimiters because of the -N
flag? Is this a bug, or intended behavior?