Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top