Elegance of Reason

Tuesday, December 06, 2005

Portability Interlude

In the previous post, the syntax ${2:-1} was used in the implementation of die() in the usual sense of providing a default value of 1 if parameter $2 is null or unset.

According to The Single UNIX Specification, the above syntax is the standard-blessed way to obtain the intended effect; according to the Autobook, however, the notation ${2-1} would be more portable. Experimentation with a few sh-derived shells, shows that none chokes on either syntax; on the matter of portability it is always wise to acknowledge consolidated practice, so the implementation of die() is hereby changed accordingly.


warn() {
echo 1>&2 "$0: $1"
}

die() {
warn "$1"; exit ${2-1}
}

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home