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

前端 未结 2 1563
甜味超标
甜味超标 2021-02-15 02:05

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 b

2条回答
  •  北海茫月
    2021-02-15 02:43

    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.

提交回复
热议问题