Back to the index
Portable shell programming, part 1
Writing shell scripts that work on almost every Unix-like operating system is not a trivial task, but it is clearly possible. This article summarizes some pitfalls that were collected on May 18 2005 from the CVS ChangeLog of the pkgsrc/mk directory from the NetBSD project.

if ! command; then ...; fi

The Solaris shell does not understand this pattern. As a workaround you should use:

if command; then :; else ...; fi

for i in ${DEPENDS}; do ...; done

When this code appears in a BSD Makefile and the DEPENDS variable is empty, the shell gets this code:

for i in ; do ...; done

In the Solaris shell, this is a syntax error. A workaround is:

depends=${DEPENDS:Q}; for i in $$depends; do ...; done

The latter pattern also has the advantage that the DEPENDS string may contain characters like <>&`'". But pay attention to shell wildcards. They are expanded, as the $$depends in the for loop is unquoted.

$(command)

The Solaris shell does not know this alternative to `command`. So just use the backticks.