do you use sh or bash for scripts?

I've often heard people say that you should use #!/bin/sh in your shell scripts to ensure they run everywhere.

I always use /bin/bash - because I like the bash-specific features and wouldn't like to live without, and I don't have a list in hand that shows me which "features" I can't use if using /bin/sh - so I can't be sure my script actually works on generic /bin/sh - without testing.

What do I mean without testing? If I use bash-specific features (like $!VAR ) they still work, as long as /bin/sh is actually using bash, even though it is my understanding that bash should emulate the sh posix standard, it obviously extends it. This is a major bummer, as my script breaks if I accidently use some bash-specific feature.

I recently saw this, on some Linux distro's that use dash to emulate /bin/sh - and it does not support the $!VAR notion from bash, among other things (I had, by mistake, put #!/bin/sh in the script).

If you use /bin/sh - how do you make sure, you only use the actually supported features - do you have a testbed, or a list of "no-no's" ?

0 comments on do you use sh or bash for scripts?

  1. Anonymous
    Tue, 06/02/2009 - 11:53
    in the rare cases where someone needs it on a system that doesn't have bash, porting it is no big deal, usually. I regard writing and TESTING that a script really does work for plain sh is not worth the time spent in general. Exceptions apply, of course. My further opinion is that unless you are going to test that it actually works with /bin/sh, it's better to be honest and say /bin/bash. For extra points you can write a line that it requires bash because it is untested outside of bash.
  2. Anonymous
    Tue, 06/02/2009 - 10:09
    Except for pipefail (which Sune mentioned) I very rarely have the need for bash extensions. If I need more advanced features I'm usually switched to perl before I need them. //Makholm
  3. Anonymous
    Tue, 06/02/2009 - 09:24
    I try to make my scripts posix compliant. I usually run my scripts in dash, as that's my /bin/sh. I do though, when I really need it, script for /bin/bash, but usually only when I need "pipefail". The $!VAR and other such constructs I don't use, as I don't think they are easy readable. /Sune