can't define custom exit function

Bug #301225 reported by Mikel Ward
2
Affects Status Importance Assigned to Milestone
dash (Ubuntu)
New
Undecided
Unassigned

Bug Description

I had my own version of the exit function declared in one of my shell startup files. It worked in bash and zsh, but not in /bin/sh on Ubuntu.

Adding to the problem, it seems GDM uses /bin/sh inspite of my shell being /bin/zsh.

$ cat exittest
#!/bin/sh

exit()
{
        case $- in *m*)
                # this way works in bash and zsh
                jobs | wc -l | grep -q '^0$'
                if test $? -eq 0
                then
                        command exit "$@"
                else
                        jobs
                fi
        esac
}

exit
$ ./exittest
./exittest: 3: Syntax error: Bad function name
$ dpkg -S /bin/sh
diversion by dash from: /bin/sh
diversion by dash to: /bin/sh.distrib
bash: /bin/sh
$ dpkg -l dash | grep '^i'
ii dash 0.5.4-9ubuntu1 POSIX-compliant shell

Revision history for this message
Jilles Tjoelker (jilles) wrote :

I think the real problem is in gdm here: if it wants to read the user's shell startup files, it should do so using the user's login shell, not /bin/sh. User shell startup files cannot be guaranteed to even parse in /bin/sh, and changing /bin/sh to make it work degrades sh -n for actual /bin/sh scripts.

As a workaround, use . or eval to pull in the extension-using code if the extensions are supported.

POSIX does not say whether it is allowed to define a function with the same name as a special builtin such as exit, but does say that it is impossible to call such a function. This is a feature as it makes sure that if something looks like a special builtin (after alias expansion), it is one. For example, unset cannot be overridden with a function, which allows using it to remove functions covering all other utilities.

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.