views:

215

answers:

7

Is it better practice to write Bash scripts or Bourne scripts? My team writes Bourne scripts but I am not entirely sure why.

If this is a holy war question (ie: vim vs. emacs) please just reply: holy war.

+11  A: 

You can be more sure that Bourne shell will be installed on any given Unix computer. Yeah, Bash is ubiquitous on Linux, but the whole world isn't Linux.

Paul Tomblin
Somewhat along those lines, I can think of only three reasons: backward compatibility, portability, or "that's the way we've always done it."
Joe Suarez
"Bash is ubiquitous on Linux, but the whole world isn't Linux." This was my belief before, then i started working with Solaris and HP-UX machines. I thought i could write shell scripts in Bash for these machines, oh how wrong I was. But it was a good humble learning experience, now I always use the bourne shell and only syntax that I know is POSIX compliant.
Anders
+4  A: 

Well, is a matter of taste, but for starters, bourne shell scripts can be run with bash, and I think bash has features that cannot be run by Bourne.

Francisco Soto
+8  A: 

It depends on what your target platform is.

If you're only targeting, say, major Linux distributions and Mac OS X, then you can be confident that these systems will have bash available. On other UNIXes (e.g., AIX, Solaris, HP-UX), bash may not necessarily be present, so Bourne is the safer choice. If bash is available, I can think of no reason you might prefer Bourne.

Ray
+8  A: 

The most important thing is to remember that not every OS softlinks /bin/sh to /bin/bash, as some Linux distros do. A lot of scripts are written for bash but begin with:

#!/bin/sh

so that they break e.g. in Ubuntu. So, when you write bash script, always write:

#!/bin/bash
el.pescado
One of my cow orkers was mad at Ubuntu because they used something other than bash for /bin/sh. I told him he should be mad at the idiots who mislabeled their bash scripts as sh scripts, but he wouldn't agree with me.
Paul Tomblin
A: 

I'd go for bourne again shell, as the bourne shell can be slightly different among unix implementations. With bash you can be sure that bash is always bash.

Patrick
I wouldn't say that different shells operate differently. Any shell I know will fall back to bourne shell behavior if it is started as `sh`
soulmerge
I have seen different bourne shell (sh) implementations that behave differently. Maybe this is not the case anymore.
Patrick
"With bash you can be sure that bash is always bash." Unless it's a different version of Bash. There are obviously significant differences between major version numbers, but there are also some between minor version numbers.
Dennis Williamson
@Dennis: you're right (+1). Thanks for pointing this out.
Patrick
...but you're right that Bourne (at least what passes for it) can be "more different".
Dennis Williamson
A: 

Portability. I write #!/bin/sh unless things get really to painful, and then I write #!/bin/bash. The world is changing very rapidly, and I'm betting that in the future it will be easy to convince sysadmins to install bash. But I hedge my bets by using Bourne for most stuff, which is simple.

Norman Ramsey
+1  A: 

On Mac OS X /bin/sh is NOT a Bourne shell. (But you may get a true bournesh over at freshmeat).

To identify a traditional Bourne shell you may try to use the circumflex ^ (caret) as a replacement for | (pipe).

See:

The Traditional Bourne Shell Family,

http://www.in-ulm.de/~mascheck/bourne/

yabt