Shebang

Hi,

I am currently writing BASH shell scripts. I am using BASH on a Powerbook G4 running Leopard. Could somebody please explain the difference between
#!/bin/bash and #!/bin/sh?

I have been using the latter (#!/bin/sh), and things have been working fine. But is that the correct one to use for the version of BASH that is on Leopard? Also, I plan on giving the script to somebody running Ubuntu. Which shebang would they want to use?

Mike

/bin/sh is usually just a link to or another copy of bash which is included for legacy Bourne shell scripts. (ETA - I say "usually" when referring to Linux, and probably MacOS, which I haven't used).

If the script you are writing relies on some bash-specific features you should probably refer to /bin/bash instead.

Hi,

Thank you for the reply. So I understand that bash is basically a revamped bourne shell. Does that mean a script written for a bourne shell could be interpreted by a bash shell, but PERHAPS not vice versa? How similar are the two shells?

Also, if /bin/sh is just a link to, or a copy of bash, why would it be necessary to use /bin/bash for scripts that need bash-specific features? It seems like they are the same. Or perhaps is it possible on some machines that /bin/sh is actually the bourne shell?

Mike

Bash is upwards-compatible with "classic" sh, see the manual page for what's changed. It also mentions what's specific to Bash and cannot be expected to work in traditional Bourne shell.

For scripts you only use yourself, it doesn't matter what you put on the shebang line; use whatever works for you. The difference enters when you want to share your scripts with others. It's a useful convention, though, to explicitly use /bin/bash in scripts which you know are only Bash-compatible, and /bin/sh in scripts which you want to be generally Bourne-compatible.

In general, the expectation is that /bin/sh is Bourne-compatible, but on many platforms, it is indeed not Bash.

Consider also the following:

man bash|less -p'name sh'

Hi,

Thank you for all the help. I really appreciate it. Just a few last questions:

  • Since bash is upward compatible, should I expect any issues with my script if I change the shebang to #!/bin/bash?

  • Although I can have my script running when it invokes bash with sh, I wonder if it will work on the system for my client, for whom I am writing the script. That's what makes me think I should just change the shebang to #!/bin/bash because he has indicated he has bash. He is running Ubuntu. I know some systems store bash in different places. Would #!/bin/bash work on his system?

You should consider using the below syntax if you're using bash specific syntax:

#!/usr/bin/env bash

If your aim is portability you should write a POSIX compliant code.

My understanding is that /bin/sh is the "Bourne shell" and /bin/bash is the "Bourne again shell". What this means is, "sh" is a subset of "bash", therefore anything coded under "sh" should in theory work with "bash". The reverse is not necessarily true.

Yep, the name bash is an abbreviation for Bourne again shell.

And yes, /bin/bash should exist on any vanilla Ubuntu installation; but I concur with previous comments that it might be best if you could write properly sh-compatible scripts. On recent versions of Ubuntu /bin/sh is a symbolic link to /bin/dash which is a much less featureful, Bourne "classic"-compatible shell.