使bash脚本在Linux和FreeBSD之间可移植的正确方法是什么? [英] What is the proper way to make a bash script portable between Linux and FreeBSD?
问题描述
我正在研究一些bash脚本,这些脚本可以在我的Linux和FreeBSD系统上使用.
I am working on some bash scripts that I'd like to work across my Linux and FreeBSD systems.
由于我主要在Linux上工作,所以我习惯于以以下方式启动bash脚本
Since I mostly work in Linux, I am used to starting my bash scripts with
#!/bin/bash
但是这在FreeBSD上不起作用,因为bash位于/usr/local/bin/bash
.因此在FreeBSD上,我的脚本需要以
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.
推荐答案
#!/usr/bin/env bash
如果pash在路径上的某处,则
应该可以解决问题.有关更多详细信息,请参见此处.
should do the trick, provided that bash is on the path somewhere. See here for more details.
这篇关于使bash脚本在Linux和FreeBSD之间可移植的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!