On 03/11/2011 10:03 AM, Roger N. Clark wrote:
Tail with a line count option with multiple files fails.
For example:
tail -3 foo.txt
f
d
s
works, but
tail -3 foo.txt csh.cshrc
tail: option used in invalid context -- 3
Thanks for the report. At first, I was about to claim that this is not
a bug in GNU coreutils, but a difference in levels of POSIX compliance.
But digging further shows this in the NEWS file: Back in coreutils 5.90:
** Bring back support for `head -NUM', `tail -NUM', etc. even when
conforming to POSIX 1003.1-2001. The following changes apply only
when conforming to POSIX 1003.1-2001; there is no effect when
conforming to older POSIX versions.
tail -[NUM][bcl][f] [FILE]
Basically, POSIX 2001 and newer does _not_ require -3 to be parsed as a
valid option (rather, the POSIX spelling is -n -3), so GNU coreutils is
supporting -3 as an extension.
Then I looked at the actual POSIX 2008 wording:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/tail.html
tail [-f] [-c number|-n number] [file]
That is, POSIX states that it is only portable to supply at most one
filename, so the fact that GNU tail can support multiple files is
already an extension, whether or not you also mix in the GNU extension
of supporting -3 as an option.
You can work around it by using: 'tail -n -3 file1 file2'
This works on HP-UX.
Since we're already dealing with two GNU extensions, I don't see why we
can't be nice and make the shorter syntax work the way HP-UX is doing
things. Patches welcome!