3

My company is starting to grow our Linux support and some of our shell scripts are starting to get more complex. Today we use the shebang as

#! /bin/sh

but we are having problems with some distros like Ubuntu for example where sh points to dash and some more exotic.

My question is, what is the risk in changing the shebang to bash for example? Maybe the better question is if it is possible to have a distro without bash these days?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mac
  • 3,397
  • 3
  • 33
  • 58
  • Are you thinking desktop Linux distros or embedded Linux distros? Most desktop systems will have `bash`; smaller and more specialized system may not. – Jonathan Leffler Dec 13 '12 at 00:13

3 Answers3

5

Better to change it, if you're relying on bash specific syntax then your shell script needs to state that it needs bash and not a generic shell.

#! /usr/bin/env bash 

is usually preferred to

#! /bin/bash

as it allows for a system to have bash installed in a non-standard way/location

In answer to your second question: "is it possible to have a distro without bash these days". Well, of course it's possible, however it's incredibly unlikely. From the maintainer-of-bash's website -- bash 4.x is standard on GNU/Linux and Mac OSX, on windows both Cygwin and MinGW ship with bash 3.x. There are ports for BSD, and Solaris has come with versions of bash since 8+, with 10 shipping with bash 4.x.

That seems to suggest you would be incredibly unlucky to find a GNU/Linux OS without bash.

Dunes
  • 37,291
  • 7
  • 81
  • 97
  • On the other hand, between the lines I suppose they would like for their scripts to work on systems where Bash is not installed. Then they should weed out the bashisms from their scripts, and keep the `#!/bin/sh`. – tripleee Dec 13 '12 at 05:19
  • @tripleee Bash seems to be so common that it's more hassle to pre-emptively do this than. Perhaps it's more worth considering whether or not to force bash4 rather than any version of bash. – Dunes Dec 13 '12 at 07:29
  • @tripleee (or anyone): do you know of a decent, simple list of bashisms? – Michael Burr Dec 13 '12 at 22:46
  • http://www.gnu.org/software/bash/manual/bashref.html#Bash-Features and in particular http://www.gnu.org/software/bash/manual/bashref.html#Bash-POSIX-Mode is a good start. – tripleee Dec 13 '12 at 22:58
4

I think you are asking the wrong question. The better question is: "How bad is it to use #!/bin/sh for scripts that use non-portable shell syntax (aka, bashisms)". To which the answer is: "really bad". Fix your shebangs. If you write #!/bin/sh but write a bash script, you are asking for trouble. Stop thinking of dash as exotic, and you'll write more portable code. In short, it is not at all bad to fix your shebangs, and it is very bad to not do so.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
1

No more risk than changing it to PHP. Or to Perl. Or to Python. Use any interpreter you like.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • 2
    maybe the question should be what is the risk of finding a distro without bash these days? – Mac Dec 13 '12 at 00:08