How do I get a bash script working on FreeBSD, OpenBSD and Linux without modifying it?
Asked Answered
S

2

9

Sorry, the headline might be a bit irritating, but I didn't know anything better. Anyway, I want a bash script to work on FreeBSD, OpenBSD and Linux without modifying it, but bash isn't located at the same place in Linux and BSD.

So, if I write #!/bin/bash then it won't work on BSD, because the bash shell is located in /usr/local/bin/bash there. Is there any solution to get this script working on both?

Or do I really need to ship two scripts with different paths...?

Sheng answered 12/7, 2013 at 12:12 Comment(1)
Also note that on a FreeBSD system there's no guarantee whatsoever that bash will be available at all, it's an optional install...Precautious
Q
15

Using env in the shebang (#!/usr/bin/env bash) should make the script OS agnostic.

Quadruplet answered 12/7, 2013 at 12:16 Comment(0)
I
4

I like the answer about using #!/usr/bin/env bash It is an interesting and excellent answer, but that would only work if bash is in the path.

Another option might be to use #!/bin/sh which is the most universally compatible shell location. Then, have the script do something in sh, such as check where bash is installed (if bash is even installed). Another option might be to have bash exist to both locations. Making another installation may sound like overkill, but this goal could be accomplished as simply as creating a hard link so that bash actually exists in both locations.

Instillation answered 29/10, 2013 at 21:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.