3

I'm writing a Debian/Ubuntu service that should start on at runlevel 2-5.

Most of the existing services I came accross are written using /bin/sh (Shell) interpreter while /bin/bash (Bash) offer lot of improvement and simplification.

So is there good reasons to avoid the use of bash, when targets are limited to debian 6+ and ubuntu 12.04 ?

Update:

I usually add the following shebang to my scripts: #!/usr/bin/env bash

Édouard Lopez
  • 425
  • 1
  • 3
  • 13
  • https://wiki.ubuntu.com/DashAsBinSh is probably worth a read. `dash` is meant to have a small footprint and implement the functionality required of a POSIX shell and not much more. `bash` has a lot of extensions and a much larger footprint. If you write your scripts to work with `dash` rather than `bash` they are more portable (will work on pretty much any system regardless what shell `/bin/sh` actually is on that system). – Håkan Lindqvist Jul 17 '14 at 10:32
  • I defined `bash` as a requirement, but is it available on *BSD, on various runlevels, etc. ? – Édouard Lopez Jul 17 '14 at 10:48
  • 1
    That may depend on which BSD you are looking at but traditionally they do not use `bash` at all, rather `tcsh` and/or `ksh` for user shells and iirc at least FreeBSD has `ash` as `/bin/sh`. Anyway, unless I'm mistaken neither FreeBSD nor OpenBSD have `bash` in their base install at all (it can be installed from ports), much less as `/bin/sh` – Håkan Lindqvist Jul 17 '14 at 11:45
  • The BSDs do not come with Bash as a default. It can be installed as an option after the fact. DO NOT bash the system default shell. – Walter Apr 28 '15 at 00:18
  • FYI, the BSDs do not use runlevels (that was something that Linux imported from System V designs). – Walter Apr 28 '15 at 00:21

3 Answers3

2

There are no reasons to avoid the use of bash in startup scripts. If you are comfortable with bash, and you like it because it offers functionality you don't get in another shell, by all means use it. First, make sure your /bin/sh isn't actually a link to /bin/bash or a copy of /bin/bash itself - on some systems this is the case.

Michael Martinez
  • 2,543
  • 3
  • 20
  • 31
1

Bash might not be installed.

Unlike other distributions, Debian and Ubuntu use dash, a stripped down Bourne shell, as the default /bin/sh shell.

Michael Hampton
  • 237,123
  • 42
  • 477
  • 940
1

If you specifically target systems that always come with bash (primarily the typical Linux distros(?)) I see no harm in making use of it if that allows for more maintainable scripts or some other significant advantages to you (provided of course that the shebang properly reflects the bash dependency).

dash is aiming to be a lean POSIX compliant shell. Debian and Ubuntu switched to it as a means of getting a lighter shell in the bulk of their startup scripts (leading to faster boot times, etc) as well as ensuring that all their scripts that claim to run with /bin/sh are not actually riddled with accidental bashisms.

If, on the other hand, portability is important, I think your default /bin/sh (dash) seems like a much better starting point as a script which only uses POSIX shell features will probably run on most systems, not only those with bash present. However, there are of course likely many other factors than just the shell to take into account when dealing with portability.

Håkan Lindqvist
  • 33,741
  • 5
  • 65
  • 90