Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash or Bourne Scripts?

Tags:

bash

scripting

sh

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.

like image 858
sixtyfootersdude Avatar asked Apr 27 '10 16:04

sixtyfootersdude


People also ask

Which shell is best for scripting?

Tcsh (Tenex C Shell) This open-source shell for Linux is best for programmers because its syntax is like the C language, so these users can use the scripting features in Tcsh without any knowledge of Bash.

Should I use bash or Python for scripts?

Performance-wise bash outperforms python in the process startup time. This shows a huge difference however bash execution time degrades quickly if it has to do anything sensible since it usually must call external processes. If you care about performance use bash only for: really simple and frequently called scripts.

What does Bourne mean in bash?

As explained in the Bash Reference Manual, the name bash is an acronym of "Bourne-again SHell" which is a pun on Stephen Bourne, author of the Bourne shell. Bash is a superset of the earlier shell, and generally compatible with Bourne shell programs.

Is Bourne shell still used?

Along with the Korn shell and the C shell, the Bourne shell remains among the three most widely used and is included with all UNIX systems.


7 Answers

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.

like image 158
Ray Avatar answered Oct 03 '22 21:10

Ray


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.

like image 25
Paul Tomblin Avatar answered Oct 03 '22 21:10

Paul Tomblin


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
like image 21
el.pescado - нет войне Avatar answered Oct 03 '22 21:10

el.pescado - нет войне


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.

like image 32
Francisco Soto Avatar answered Sep 29 '22 21:09

Francisco Soto


I use Bash as my login shell, but for scripting I'd choose the Bourne shell any day of the week and twice on Sunday. Bash has better features, better user friendliness and better bugs.

Actually, the same stuff that makes me choose Bash when I'm logging in, makes me avoid it when scripting. Bash tries to make everything nice and cozy for the user, but at the expense of a 776 kB executable (on my machine), compared to 140 kB for Bourne shell. Why would my script care about user friendliness? Any gain I might achieve through the use of some clever Bash function is effectively cancelled out by the shell footprint, which is more than five times as big.

I have computers running Linux, FreeBSD and OS X. Although I rarely move anything between the computers, it's nice to have the possibility. In a Bourne shell script, you simply type

#!/bin/sh

and it just works. Always. Bash might be common on Linux, but it's not as standardized as the Bourne shell. On FreeBSD, Bash is not installed by default. It can be installed from Ports if the sysadmin thinks it's a good idea but, even then, it ends up in /usr/local/bin/bash (not /bin/bash). Thus, if you still decide to go with Bash, you should write

#!/usr/bin/env bash

to make the script portable. env will find the shell for you, regardless of your Unix flavor (as long as it's installed).

At the end of the day, it's your choice. Just make sure that your scripts are actually compliant to the shell you choose, and not relying on "sh" being symlinked to "bash" or something similar.

like image 38
Anders Sjöqvist Avatar answered Oct 03 '22 21:10

Anders Sjöqvist


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.

like image 43
Norman Ramsey Avatar answered Sep 29 '22 21:09

Norman Ramsey


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/

like image 35
yabt Avatar answered Oct 03 '22 21:10

yabt