Why I hate the Solaris 10 version of /bin/sh

August 23, 2010

Every so often I discover some exceptional piece of braindamage in Solaris. Here is one of them, presented in handy illustrated form:

$ cat cdtest
#!/bin/sh
cd /not/there || echo failed
echo got here

$ sh cdtest
./cdtest: /not/there: does not exist

(No other output is produced.)

Oh yes, Solaris. Killing my script when a cd fails is just what I want you to do, especially when this behavior is undocumented and cannot be turned off. I especially like it when you do this despite me making every attempt to handle the error.

It goes without saying that this behavior is in no way POSIX standard and in no way matches either the historical behavior of the Bourne shell or how the Bourne shell behaves on other systems.

I think that all of our Solaris shell scripts are about to change from '#!/bin/sh' to '#!/usr/bin/bash'. I'm not a huge fan of Bash, but at least it doesn't contain helpful landmines that blow up production scripts.


Comments on this page:

From 78.94.216.81 at 2010-08-23 14:03:26:

Yeah, solaris and /bin/sh ...

I remember a funny dialog between a solaris admin and myself:

'Hm, funny that Solaris 10 /bin/sh does not support ~/ expansion - it is even a posix feature ...'

'How do you get the braindamaged idea that /bin/sh should be a posix shell?!? The very obvious posix standard conforming portable way to get a posix conforming shell is via

PATH=`getconf CS_PATH` sh.

This is not linux! Changing /bin/sh to something more conforming could break compatibilty for old scripts! ...'

Well, I smiled a bit and expected that this rant would end with something like 'Heil Solaris backward compatibility!' - which did not happen ... ;)

From 70.103.74.8 at 2010-08-23 14:21:24:

I don't have a Solaris server on which to test but the behavior you're seeing may not be the default; rather somebody has "set -e" (exit immediately if a command exits with a non-zero exit status) in a a profile file.

Nevertheless the POSIX shell standard is clear that "set -e" should not be enabled by default. See http://www.opengroup.org/onlinepubs/009695399/utilities/set.html : "The default for all these options shall be off (unset) unless stated otherwise in the description of the option or unless the shell was invoked with them on."

By cks at 2010-08-23 14:54:48:

This is not a default 'set -e' in action; it happens only for cd, not for other commands, and it doesn't stop if you do 'set +e'.

From 130.233.200.85 at 2010-10-29 06:58:16:

I'd suggest ksh instead of bash, here. So much less pain and it's obviously Sun's own tool of choice when sh fails.

The funny thing is, this has been reported and verified as bug against SunOS 4.1.3 U1 and Solaris 2.3, but it hasn't been fixed, just closed. Csh and tcsh are affected too, although why anyone would care lies beyond the limits of my imagination.

Bug ID: 1171176 Synopsis: sh bourne shell scripts exit if "cd some_nonexistent_directory" Category: shell; Subcategory: bourne; State: closed

Maybe I'll open a support case just for the fun of it, some time.

-- jk at tkk dot fi

Written on 23 August 2010.
« My (probably wrong) assumption about Flash on Fedora
A design mistake in my comments form »

Page tools: View Source, View Normal.
Search:
Login: Password:

Last modified: Mon Aug 23 11:56:39 2010
This dinky wiki is brought to you by the Insane Hackers Guild, Python sub-branch.