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

Asked
Active3 hr before
Viewed126 times

5 Answers

working
90%

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?,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.,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.

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

env

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

#!/usr/bin/env bash
load more v
88%

So if I want to use a Linux script in FreeBSD, and I run ./script.sh in the shell, it will run the Bash script in "sh" and not Bash, since /bin/sh in FreeBSD is not Bash.,In FreeBSD, /bin/sh is not bash, it's the true sh,Bash script usually #!/bin/sh If they rely on non-standard bash features, it is highly recommended to have a #!/bin/bash shebang,Linux script are written in Bash

You can call your favorite shell with the script as a parameter.

bash. / script.sh
load more v
72%

NetBSD and OpenBSD also ship with the C shell as their default shell. However, it is not always the same tcsh, but often its simpler variant, csh, which doesn’t support all of the tricks provided in this hack.,Let’s start with this variant, which I found as the result of a Google search:,FreeBSD comes with lock (and it’s available for NetBSD and OpenBSD). Its default invocation is simple:,FreeBSD and OpenBSD ports also include net/unison.

% so
% soc
% sockstat
load more v
65%

How UNIX® file permissions and FreeBSD file flags work.,For a detailed description of every column in this file and the available options for the virtual consoles, refer to ttys(5).,In FreeBSD, the number of available virtual consoles is configured in this section of /etc/ttys:,The settings which control single user mode are found in this section of /etc/ttys:

FreeBSD / amd64(pc3.example.org)(ttyv0)

login:
load more v
75%

Linux scripts are written in bash: Well, some are written for bash, others for dash and hopefully a lot potable for any POSIX-compliant shell.,Bash script usually #!/bin/sh If they rely on non-standard bash features, it is highly recommended to have a #!/bin/bash shebang,So I only see the possibility to have a symbolic link from /bin/sh to your zsh. But I don't recommend to do this, as it may have drastic impact on your boot time and will not even help you for some of your linux scripts.,You can call your favorite shell with the script as a parameter.

You can call your favorite shell with the script as a parameter.

bash. / script.sh
load more v

Other "working-undefined" queries related to "How do I get a bash script working on FreeBSD, OpenBSD and Linux without modifying it?"