Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I assume bash is installed

Tags:

linux

bash

debian

I have a cross platform product that gets deployed to several Linux distros and I'm busy changing one of its startup scripts. Currently the script uses /bin/sh as interpreter. My question is. How safe am I if I change this to /bin/bash, will there be some cases that bash is not installed or something. Why does all the scripts in /etc/init.d use /bin/sh. Are there some stuff that will break when using bash?

like image 860
Hannes de Jager Avatar asked Jan 27 '11 08:01

Hannes de Jager


2 Answers

Can I assume bash is installed

If portability is a goal, you would be doing yourself a disservice to assume bash was installed. That is not to say that bash won't be installed 99% of the time, but it's the 1% that gets ya.

How safe am I if I change this to /bin/bash, will there be some cases that bash is not installed or something

bash is a POSIX Compliant shell, so unless you need/want to use bash-only syntax, I would not just change the interpreter line to #!/bin/bash on a whim; it buys you nothing with a script that works with #!/bin/sh

Why does all the scripts in /etc/init.d use /bin/sh

Because they don't want the system to go down if the admin decides he wants to save hard drive space by deleting bash because his favorite shell is zsh

Are there some stuff that will break when using bash?

It's more the other way around. If you have a script with bash-only (non-POSIX) syntax, for example [[ ]] or process substitution <( ), this will break if you change the interpreter from #!/bin/bash to #!/bin/sh

like image 132
SiegeX Avatar answered Nov 15 '22 07:11

SiegeX


It depends on how "cross" platform you application is.

In case it runs on "major" linux distributions, feel free to assume bash, but in case of smaller dists, or customized installs bash is not a given. Indeed if you move away from linux i would be even more careful about assuming bash (and for that matter the /bin/bash location is not a given either).

The reason why /etc/init.d scripts always use /bin/sh is because it is a given on most platforms.

I don't think stuff will break if you use bash, bash implements the same "features" as sh, but not the other way around. So in short if you don't need bash specific features, then use /bin/sh

like image 35
Esben Bach Avatar answered Nov 15 '22 07:11

Esben Bach