#!/bin/bash on FreeBSD
A great number of people write bash scripts for Linux.
Most of these scripts start with #!/bin/bash
.
bash is not installed on FreeBSD by default.
When installed, it is installed in /usr/local/bin/bash.
This post covers a few strategies for using scripts that start with #!/bin/bash
on FreeBSD.
I used one of these strategies in a previous post.
Software Versions
$ date -u "+%Y-%m-%d %H:%M:%S +0000"
2016-02-20 17:04:07 +0000
$ uname -vm
FreeBSD 11.0-CURRENT #0 r287598: Thu Sep 10 14:45:48 JST 2015 root@:/usr/obj/usr/src/sys/MIRAGE_KERNEL amd64
Instructions
First, bash needs to be installed.
portmaster shells/bash
The simplest option is to create a symlink from /usr/local/bin/bash to /bin/bash.
su
ln -s /usr/local/bin/bash /bin/bash
At this point, the following script should work without modification.
hello_world.sh
#!/bin/bash
echo "Hello, World!"
If you object to adding a symlink, the following command can be used to
replace #!/bin/bash
with the more portable #!/usr/bin/env bash
.
FILENAME=hello_world.sh
sed -i '' 's:^#!/bin/bash:#!/usr/bin/env bash:' "${FILENAME}"
If a large number of files need to be changed, the following can be used to recursively patch all files in a directory.
DIRNAME=.
find "${DIRNAME}" -type f -print0 | xargs -0 sed -i '' 's:^#!/bin/bash:#!/usr/bin/env bash:'