1

I have written a shell script with #!/bin/sh shebang. I've heard that it won't work alongside with Solaris. And that I have to write a korn shell script.

But in one of the threads an individual mentioned that it is possible to make the transition by simply changing the shebang on top.. Which lets it point to a different directory in Solaris where sh is present.

I have no way of knowing if this is true and I would love it if you'd clear it up for me.

Will this method work? And is this the best way?

  • You need to clarify your question; usually solaris /bin/sh isn't bash but that doesn't mean you must write a script in ksh. For one thing, what version of solaris and does it have bash? Try #!/usr/bin/env bash – Elliott Frisch Oct 24 '14 at 01:58
  • 1
    Why not just try it? – nobody Oct 24 '14 at 02:21
  • Try it, and if it doesn't work, you can come back and ask about any specific errors you don't understand. – chepner Oct 24 '14 at 02:59
  • Every UNIX-like system has a shell at /bin/sh. If you avoid features specific either to bash or to ksh, there shouldn't be a problem. – Keith Thompson Oct 24 '14 at 05:07
  • @KeithThompson That will be a problem with Solaris. The expected answer is to use #!/usr/xpg4/bin/sh unfortunately, I can't post it as a reply as the question has been closed. – jlliagre Oct 24 '14 at 21:49
  • @jlliagre: /bin/sh does exist on Solaris. Why would using it be a problem? Is it an older version of the shell? – Keith Thompson Oct 24 '14 at 22:00
  • @KeithThompson That's indeed the problem. /bin/sh is the original non POSIX legacy Bourne Shell. It breaks with most current /bin/sh scripts thus /usr/xpg4/bin/sh should be used. – jlliagre Oct 24 '14 at 22:01

2 Answers2

2

Assuming your script is portable, i.e. doesn't use bashisms, GNUisms, or whatever non POSIX, it should work with most Unix and Linux OSes with its #!/bin/sh shebang.

However under Solaris 10 and older, /bin/sh is not the POSIX shell but the legacy Bourne shell which predates POSIX and is then missing features that came later with the standard.

Your script is then likely to break if left as is but this can easily be fixed by setting the shebang to point to the POSIX compliant Solaris shell this way:

#!/usr/xpg4/bin/sh

To make sure you'll also pick the POSIX compliant utilities when they differ from the default Solaris ones, you should also put the following line early in your script:

PATH=$(getconf PATH):PATH
jlliagre
  • 61,204
-1

Yes you are right.

In solaris and Linux it is different so if we need to write a single script then we can use this way.

#!/usr/bin/env sh

< Your Code>

This will work.

In Linux.

$which sh
/bin/sh

In Solaris.

$which sh
/usr/bin/sh

Just to avoid this paths in single script we can use envcommand.

  • 1
    Not sure why this was accepted as this is not the correct reply. On Solaris, /usr/bin is a symbolic link to /bin so #!/bin/sh and #!/usr/bin/sh work identically . – jlliagre Oct 24 '14 at 21:46
  • @jlliagre: As I mentioned I have no means of testing it. Please do leave a response stating the way in which a shell script could be made compatible to run in a Solaris system – Naveen Dennis Oct 25 '14 at 16:03