[PATCH] Use POSIX standardized options for head(1) and tail(1)

Michael Forney mforney at mforney.org
Sun Apr 26 17:46:42 AEST 2020

On 2020-04-25, Mark D. Baushke <mdb at juniper.net> wrote:
> Hi Michael,

Hi Mark,

> Using POSIX command-line semantics is a fine goal. Someone might drop
> the backward-compatiblity of the commands and then things would begin to
> break.

Indeed, that's what motivated the patch. The version of tail on my
system does not support the legacy options.

> However, there is an alternative. Do not use these POSIX commands
> that have changed their command-line syntax if one that exists and is
> more stable is also available.
>> @@ -353,7 +353,7 @@ depend-rebuild:
>>         rm -f config.h
>>         touch config.h
>>         makedepend -w1000 -Y. -f .depend *.c 2>/dev/null
>> -       (head -2 .depend; tail +3 .depend | sort) >.depend.tmp
>> +       (head -n 2 .depend; tail -n +3 .depend | sort) >.depend.tmp
>>         mv .depend.tmp .depend
>>         rm -f config.h
> I am speaking of the use of 'sed' which has been pretty stable since at
> least 1992 and mostly the same going back to 1974.
> Using sed for compatibility with all possible portable releases seems
> 'better' to me than just upgrading a command because POSIX thinks it is
> a good idea.

This seems like a fine option to me.

> For example:
>    head -2 == head -n 2 == sed 'n;q'

Another possibility is `sed 2q`, which works for any number. It's even
mentioned in the rationale section as a way to simulate head(1).

>    tail +3 == tail -n +3 == sed '1,2d'
> The sed program has been around a lot longer without these command-line
> semantics changing for POSIX.
> These idioms should work fine going back to at least the BSD version of
> sed from 1992 by Diomidis Spinellis and maybe even going back to Lee E.
> McMahon's version in 1974.
> @@ -353,7 +353,7 @@ depend-rebuild:
>         rm -f config.h
>         touch config.h
>         makedepend -w1000 -Y. -f .depend *.c 2>/dev/null
> -       (head -2 .depend; tail +3 .depend | sort) >.depend.tmp
> +       (sed 'n;q' .depend; sed '1,2d' .depend | sort) >.depend.tmp
>         mv .depend.tmp .depend
>         rm -f config.h
> The idea of the OpenSSH portable release is to be portable across a LOT
> of UNIX (and even some non-UNIX releases). If that is the goal, why
> enforce the need to fetch and build POSIX-compliant versions of head and
> tail for very old systems?

I think this script is only necessary to rebuild the .depend file,
which is not usually necessary, but you make a good point. I didn't
realize that some implementations did not support the -n option. The
change you suggested sounds good to me.


More information about the openssh-unix-dev mailing list