can't define custom exit function
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
if test $? -eq 0
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
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.