Programmatically detecting that /bin/sh is actually csh

As my last post shows, if you write something up about a subject people will find your site no matter what the purpose of the site, or in this case blog, is about.

Therefore I'm going off on another tangent with a subject that I don't have any other place to put in, and that involves UNIX shells.

In my 'day job' we encountered a few customers that, 'for convenience' decided that they like csh so much that they want this to be the default shell. And they do this by making /bin/sh a symlink to /bin/csh, or even just copy /bin/csh over /bin/sh. This is probably most likely to happen on Linux, as that doesn't use /bin/sh as heavily as the likes of AIX, HP-UX and Solaris.

In my opinion what these people do is a very bad idea, as software should be able to depend that the syntax for /bin/sh is always the same. It maybe a superset when it's actually KSH or BASH, but it should certainly execute the minimal Posix shell syntax. Still, we have to write software that could function anyway. Sigh.

Here's what my colleague Nils came up with:

#!/bin/sh

[ "$shell" != "" ] && goto csh

echo "This is Bourne Shellish."
exit 0

csh:
echo "This is C Shell. You are a naughty fellow -- please reset /bin/sh to a Bourne type shell!"
exit 1


A plain goto csh is ok but throws an error message in bourne style shells, so we prepend it with a condition that is only true in CSH. We can't use if, as that has different syntax in the two types of shell. The condition is true on CSH because it has a variable $shell whereas Bourne style shells have $SHELL instead.

Hopefully this helps somebody out there.

Next time I'll get back to a nautical subject, I promise.