On 06/05/2026 05:42, Collin Funk wrote:
Sometimes 'head -c 0' or 'head -n 0' is used by scripts to test if a
file or directory can be opened for reading. This test makes sure we
don't try to exit immediately before opening files and that we don't
stat directory arguments. E.g., the following command should not fail:

    $ head -c 0 .

Note that tail does not behave this way after Pádraig's commit from 2013:

     commit 7abf99e1907b1b05cb45eacaa98bfa73efe0ab92
     Author:     Pádraig Brady <[email protected]>
     AuthorDate: Tue Mar 26 00:36:01 2013 +0000
     Commit:     Pádraig Brady <[email protected]>
     CommitDate: Thu Apr 4 03:01:48 2013 +0100
tail: exit without reading input if would never output * src/tail.c (main): If -n0 or -c0 were specified without -f,
         then no data would ever be output, so exit without reading input.
         * tests/tail-2/tail-n0f.sh: Augment the related test with this case.

I thought this was incorrect and was going to suggest changing it.
However, after some thinking I think I agree with the behavior. Since
'tail' starts at the end of the file it is better to quit early
instead of seeking, or reading if the input file is not seekable, when
we know there will be no output.
Yes I think it's ok for head(1) and tail(1) to diverge here,
as tail -[fF] handles missing files differently.

The test looks good.

thanks,
Padraig

Reply via email to