Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to make a bash script portable between Linux and FreeBSD?

I am working on some bash scripts that I'd like to work across my Linux and FreeBSD systems.

Since I mostly work in Linux, I am used to starting my bash scripts with

#!/bin/bash

But this doesn't work on FreeBSD since bash lives at /usr/local/bin/bash. So on FreeBSD my scripts need to start with

#!/usr/local/bin/bash

So is there something else I could use that would be portable across both systems? I'd rather not maintain two versions of the scripts.

like image 274
bryan kennedy Avatar asked Mar 01 '11 20:03

bryan kennedy


2 Answers

#!/usr/bin/env bash

should do the trick, provided that bash is on the path somewhere. See here for more details.

like image 195
Brian Agnew Avatar answered Oct 16 '22 20:10

Brian Agnew


Honestly, if you want portability, invoke as /bin/sh and code to POSIX. It's less pretty, but you will run into fewer potential issues if you do.

like image 31
BadFileMagic Avatar answered Oct 16 '22 18:10

BadFileMagic