Linux-Dash Archive mirror
 help / color / mirror / Atom feed
From: Martijn Dekker <martijn@inlv.org>
To: dash@vger.kernel.org
Subject: [BUG] 'trap' is not quite POSIX compliant
Date: Sat, 13 Feb 2016 16:45:21 +0100	[thread overview]
Message-ID: <56BF4F91.7080105@inlv.org> (raw)

The 'trap' command in dash is not compliant with POSIX. According to the
spec, both '-' and any unsigned decimal integer should be accepted as an
argument meaning 'unset this trap'; dash only accepts '-'.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_28

I did testing on all other POSIX shells I know of with this little script:

	trap 'echo BYYYE' EXIT
	trap "$1" EXIT

This script will either produce 'BYYYE' or output a "command not found"
error message, depending on whether $1 was accepted as an argument for
unsetting the trap.

None of the shells are technically compliant: they only interpret an
unsigned decimal integer up to a certain value as 'unset this trap'; a
value exceeding that is interpreted as a command.

Interestingly, that maximum value differs slightly between shells. So
far, I've found:
- dash and Busybox ash do not accept any.
- bash, yash, pdksh, mksh/lksh, FreeBSD /bin/sh, and AT&T ksh "AJM 93u+
2012-08-01" accept up to 31.
- AT&T ksh "M 1993-12-28 s+" accepts up to 32.
- zsh 4.3.11, zsh 5.1.1 and zsh 5.2-dev-1 (current git) on my Mac accept
up to 33.
- zsh 5.0.2 on FreeBSD accepts up to 34.

For compatibility purposes it might seem wise to follow the majority of
implementations, accepting up to 31.

By the way, dash and zsh, as well as bash in non-POSIX mode, also accept
the counterintuitive and non-POSIX-compliant form "trap EXIT" to unset
the trap, but other shells don't: bash (POSIX), yash and ksh93 produce
an error, while mksh/lksh silently ignore that form. What a mess...

- M.

             reply	other threads:[~2016-02-13 15:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-02-13 15:45 Martijn Dekker [this message]
2016-02-13 16:08 ` [BUG] 'trap' is not quite POSIX compliant Harald van Dijk
2016-02-15 18:46 ` Chet Ramey

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56BF4F91.7080105@inlv.org \
    --to=martijn@inlv.org \
    --cc=dash@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).