On Wed, May 15, 2024 at 06:01:04PM -0400, Jeffrey Walton wrote:
> On Wed, May 15, 2024 at 3:48 PM Peng Yu <pengyu.ut@gmail.com> wrote:
>
> > No. I need to just read one character.
> >
>
> `tail -c +1` will read just one character.
I think you have the wrong command. Perhaps you meant "head -c 1"?
Whether head reads one byte, or more than one byte, will be implementation
dependent. I don't think you can guarantee its behavior across platforms.
On Debian 12, "head -c 1" appears to consume exactly one byte, but
"head -n 1" consumes a full 8k buffer:
hobbit:~$ seq 2000 | wc
2000 2000 8893
hobbit:~$ seq 2000 | { head -c 1 >/dev/null; wc; }
2000 1999 8892
hobbit:~$ seq 2000 | { head -n 1 >/dev/null; wc; }
141 140 701
On other platforms, "head -c 1" may use a buffered read.
If you want a standard command that reads 1 byte *without* reading a
whole buffer, on all platforms, I suggest "dd bs=1 count=1".
> I guess the problem is, what do
> you want to do afterwards.
This is Peng Yu. The one guarantee you have is that you will *not*
be told what they want to do afterward. Or before. Or during.